matthias-k/ipython-journal

Assertion error in savefig

tomwallis opened this issue · 5 comments

Unfortunately I now get a new error in journal.savefig():

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-9-90edce97ea93> in <module>()
      3 fig.fig.set_figheight(6)
      4 # fig.savefig('data_2014-10-27.pdf')
----> 5 journal.savefig()

/Users/tomwallis/Dropbox/Python/ipython-journal/build/lib/ipythonjournal/journal.py in savefig()
     89 
     90 def savefig():
---> 91     get_journal().savefig()
     92 
     93 

/Users/tomwallis/Dropbox/Python/ipython-journal/build/lib/ipythonjournal/journal.py in savefig(self)
     37         filename = self.get_filename(ext='.png')
     38         self.ensure_path_exists()
---> 39         plt.savefig(filename, bbox_inches='tight')
     40         print('<img src="files/{0}" />'.format(filename))
     41 

/Users/tomwallis/miniconda3/envs/py33/lib/python3.3/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
    574 def savefig(*args, **kwargs):
    575     fig = gcf()
--> 576     res = fig.savefig(*args, **kwargs)
    577     draw()   # need this if 'transparent=True' to reset colors
    578     return res

/Users/tomwallis/miniconda3/envs/py33/lib/python3.3/site-packages/matplotlib/figure.py in savefig(self, *args, **kwargs)
   1468             self.set_frameon(frameon)
   1469 
-> 1470         self.canvas.print_figure(*args, **kwargs)
   1471 
   1472         if frameon:

/Users/tomwallis/miniconda3/envs/py33/lib/python3.3/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2139                     **kwargs)
   2140                 renderer = self.figure._cachedRenderer
-> 2141                 bbox_inches = self.figure.get_tightbbox(renderer)
   2142 
   2143                 bbox_artists = kwargs.pop("bbox_extra_artists", None)

/Users/tomwallis/miniconda3/envs/py33/lib/python3.3/site-packages/matplotlib/figure.py in get_tightbbox(self, renderer)
   1611                 bb.append(ax.get_tightbbox(renderer))
   1612 
-> 1613         _bbox = Bbox.union([b for b in bb if b.width != 0 or b.height != 0])
   1614 
   1615         bbox_inches = TransformedBbox(_bbox,

/Users/tomwallis/miniconda3/envs/py33/lib/python3.3/site-packages/matplotlib/transforms.py in union(bboxes)
    719         Return a :class:`Bbox` that contains all of the given bboxes.
    720         """
--> 721         assert(len(bboxes))
    722 
    723         if len(bboxes) == 1:

AssertionError: 

/Users/tomwallis/miniconda3/envs/py33/lib/python3.3/site-packages/IPython/core/formatters.py:239: FormatterWarning: Exception in image/png formatter: 'NoneType' object has no attribute 'index'
  FormatterWarning,

<matplotlib.figure.Figure at 0x107643bd0>

Hi Tom,

do you have anything in your figure? To me it looks like the bounding box is trivial. You could also check whether plt.savefig() and plt.savefig(bbox_inches='tight') works.

btw, it might be interesting whether the tests pass for you (just run nosetests in the repository directory).

Hi Matthias,

Hmm, ok, it's something to do with my specific figure. The example from your readme works just fine.

However, it is definitely caused by journal. plt.savefig() and plt.savefig(bbox_inches='tight') are both happy. nosetests runs two tests, OK.

The figure is a seaborn plot with two panels and a legend. I will show you the code tomorrow in the lab...

Hi Tom,

I was able to reproduce your error. Actually, plt.savefig(name, bbox_inches='tight') raised an exception as well. The reason seems to be that for some strange reason, plt.gcf() (get current figure) does not work for the seaborn figure.

I implemented an keyword argument figure to savefig that can be provided with an explicit figure object. In your example, journal.savefig(figure=fig) worked for me. If it does still not work, please check back with me.

Best,
Matthias

Great, that works. Thanks Matthias.