Reputation: 8180
I use the following piece of code in an ipython notebook to save a bar graph as a .png file:
plt.savefig(filename, bbox_inches='tight')
It works on my computer and I have tried running the script on another computer. However I get the following error when I try to run it on the other machine.
AssertionError
---> 119 plt.savefig(filename,bbox_inches='tight')
C:\Python27\lib\site-packages\matplotlib\pyplot.pyc in savefig(*args,**kwargs)
---> 472 self.canvas.print_figure(*args,**kwargs)
C:\Python27\lib\site-packages\matplotlib\figure.pyc in savefig(self,*args,**kwargs)
---> 1363 self.canvas.print_figure(*args,**kwargs)
C:\Python27\lib\site-packages\matplotlib\backend_bases.pyc
---> 2054 bbox_inches = self.figure.get_tightbbox(renderer)
C:\Python27\lib\site-packages\matplotlib\figure.pyc in get_tightbbox(self,renderer)
---> 1496 _bbox = Bbox.union([b for b in bb if b.width!=0 or b.height!=0])
C:\Python27\lib\site-packages\matplotlib\transforms.pyc in union(bboxes)
---> 714 assert(len(bboxes))
AssertionError:
Removing the bbox_inches='tight' argument seems to resolve the error and saves a file but there is no picture there, only a completely blank .png file.
I have made sure that our versions of python, matplotlib and other packages are all the same. Has anyone come across this before? I'm thinking it could be a bug in matplotlib, but then that would not make sense as it works fine on my computer and we have the same versions. Any ideas or suggestions?
Upvotes: 5
Views: 6336
Reputation: 1231
In my own code I "resolved" this issue by calling savefig
on the figure, rather than from pyplot (plt.savefig()
), i.e.:
fig.savefig(filename, bbox_inches='tight')
where fig
is an instance of matplotlib.figure.Figure
. This was not an issue for me because of ipython
, but rather it originated from trying to update and draw figures in a long loop.
Upvotes: 1
Reputation: 31
I had the very same error message. I showed the image via gui and then saved it, which yielded the error. I resolved it by first saving it and only thereafter showing it.
Upvotes: 3
Reputation: 8180
The error was produced when running ipython inline.
ipython.exe notebook --pylab=inline
To fix this problem just remove the '=inline'.
Upvotes: 1
Reputation: 88118
This usually means no figures are rendered to the canvas. This also explains why, when you the argument is removed there is no corresponding image! For example:
import pylab
pylab.savefig('test', bbox_inches='tight')
Yields a similar error:
pylab.savefig('test', bbox_inches='tight')
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 471, in savefig
return fig.savefig(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1185, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1985, in print_figure
bbox_inches = self.figure.get_tightbbox(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1326, in get_tightbbox
_bbox = Bbox.union([b for b in bb if b.width!=0 or b.height!=0])
File "/usr/lib/pymodules/python2.7/matplotlib/transforms.py", line 675, in union
assert(len(bboxes))
Upvotes: 4