簡體   English   中英

將Matplotlib圖形轉換為Plotly圖形

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM