[英]Convert Matplotlib Figure to Plotly Figure
我有一个数据框:
df
Date Count_Raw Count_Total Date_Week Index_Col Game_Order
0 2008-11-30 1 3.0 2008-12-02 1 1
1 2008-11-30 1 3.0 2008-12-02 1 2
2 2008-11-30 1 3.0 2008-12-02 1 3
3 2009-01-17 1 1.0 2009-01-20 2 1
4 2009-02-08 1 1.0 2009-02-10 3 1
5 2009-03-08 1 2.0 2009-03-10 4 1
6 2009-03-08 1 2.0 2009-03-10 4 2
7 2009-07-30 1 1.0 2009-08-04 5 1
8 2009-08-30 1 1.0 2009-09-01 6 1
9 2009-09-04 1 1.0 2009-09-08 7 1
10 2009-09-13 1 1.0 2009-09-15 8 1
11 2009-10-19 1 1.0 2009-10-20 9 1
12 2009-10-25 1 1.0 2009-10-27 10 1
13 2009-10-28 1 1.0 2009-11-03 11 1
14 2009-12-12 1 2.0 2009-12-15 12 1
15 2009-12-15 1 2.0 2009-12-15 12 2
16 2009-12-30 1 1.0 2010-01-05 13 1
17 2010-01-24 1 1.0 2010-01-26 14 1
18 2010-01-28 1 3.0 2010-02-02 15 1
19 2010-01-31 1 3.0 2010-02-02 15 2
20 2010-01-31 1 3.0 2010-02-02 15 3
21 2010-02-21 1 1.0 2010-02-23 16 1
22 2010-03-19 1 1.0 2010-03-23 17 1
23 2010-04-09 1 1.0 2010-04-13 18 1
24 2010-06-18 1 1.0 2010-06-22 19 1
由此我得出了一个图:
# PLot as individual points --------------------------
fig, ax = plt.subplots(figsize=(22,8))
ax.plot_date(df['Date_Week'], df['Game_Order'], marker='o', markersize=2, mew=2)
ax.tick_params('y', colors='k')
# ax.xticks(rotation=70)
ax.set_xlabel('Date')
# ax.xlabel('Date')
ax.set_ylabel('Frequency')
ax.set_title('Weekly Games')
ax.tick_params('y', colors='k')
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)
ax.yaxis.grid(True)
# ax.set_xlim([datetime.date(2008, 9, 1), datetime.date(2012, 3, 1)])
xtick_locator = mpl.dates.MonthLocator(interval=6)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_major_locator(xtick_locator)
ax.xaxis.set_major_formatter(xtick_formatter)
xtick_locator = mpl.dates.MonthLocator(bymonth=[2,3,4,5,6,8,9,10,11,12], interval=1)
xtick_formatter = mpl.dates.AutoDateFormatter(xtick_locator)
ax.xaxis.set_minor_locator(xtick_locator)
ax.xaxis.set_minor_formatter(xtick_formatter)
plt.setp(ax.xaxis.get_minorticklabels(), rotation=90, size=10)
plt.setp(ax.xaxis.get_majorticklabels(), rotation=90, size=15)
fig.subplots_adjust(bottom=0.24)
plt.legend()
plt.show()
我想将其转换为一个plotly
图。 基于Plotly指南,我写了以下内容:
plotly_fig = tls.mpl_to_plotly(fig)
这给了我以下错误:
Traceback (most recent call last):
File "<ipython-input-415-6064541e913f>", line 1, in <module>
plotly_fig = tls.mpl_to_plotly(fig)
File "path\Anaconda3\lib\site-packages\plotly\tools.py", line 465, in mpl_to_plotly
matplotlylib.Exporter(renderer).run(fig)
File "path\Anaconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 49, in run
self.crawl_fig(fig)
File "pat\Anaconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 116, in crawl_fig
self.crawl_ax(ax)
File "path\Anaconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 147, in crawl_ax
self.crawl_legend(ax, legend)
File "path\Anaconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 167, in crawl_legend
self.draw_text(ax, child, force_trans=ax.transAxes)
File "path\Anaconda3\lib\site-packages\plotly\matplotlylib\mplexporter\exporter.py", line 211, in draw_text
style=style, mplobj=text)
File "path\Anaconda3\lib\site-packages\plotly\matplotlylib\renderer.py", line 520, in draw_text
if not mpltools.check_corners(props['mplobj'], self.mpl_fig):
File "path\Anaconda3\lib\site-packages\plotly\matplotlylib\mpltools.py", line 42, in check_corners
inner_corners = inner_obj.get_window_extent().corners()
File "path\Anaconda3\lib\site-packages\matplotlib\text.py", line 965, in get_window_extent
raise RuntimeError('Cannot get window extent w/o renderer')
RuntimeError: Cannot get window extent w/o renderer
我一直在这里和这里看,这似乎表明问题与matplotlib中的corners
函数有关。 建议将后端更改为Agg
。 我这样做是通过:
plt.switch_backend('agg')
plt.get_backend()
'agg'
但是我仍然遇到同样的错误。
有没有解决方法?
遇到相同的问题,并将其追溯到图例。 如果您注释掉plt.legend()
行,对您plt.legend()
吗? 它在这里有效-现在,我只需要找出如何添加不会崩溃的图例即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.