0

I have a dataframe:

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

From this I've produced a figure:

# 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()

enter image description here

I want to convert this to a plotly figure. Based on the Plotly guidance I wrote the following:

plotly_fig = tls.mpl_to_plotly(fig)

This gave me the following error:

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

I have looked to here and here which seems to indicate the problem is something to with the corners function in matplotlib. It was recommended to change the backend to Agg. I did this via:

plt.switch_backend('agg')
plt.get_backend()

'agg'

But I still got the same error.

Is there a workaround to this?

1 Answer 1

2

Running into the same issue and tracked it down to the legend. If you comment out the plt.legend() line, does it work for you? It works here - now I just have to find out how to add a legend that doesn't crash.

Sign up to request clarification or add additional context in comments.

4 Comments

It worked for me :) Did you find a way to add back the legend?
No, I ended up just drawing the chart from scratch using plotly.
I just found a workaround based on this example : instead of using plt.legend(), when you build the figure use the label property, e.g. call plt.plot(<whatever>, label='this_curve_name') . Then, after the figure is ready, call the following: plotly_fig = tls.mpl_to_plotly(fig) plotly_fig['layout']['showlegend'] = True py.iplot(plotly_fig)
@YuvalAtzmon You should post that as answer with the line breaks, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.