Error with bbox_inches = 'tight' in matplotlib

I use the following code snippet on an ipython laptop to save the histogram as a .png file:
plt.savefig(filename, bbox_inches='tight')

It works on my computer, and I tried to run the script on another computer. However, I get the following error when I try to run it on another 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 argument bbox_inches = 'tight' seems to resolve the error and save the file, but there is no image there, but only a completely empty .png file.

I made sure that our versions of python, matplotlib and other packages are all the same. Has anyone come across this before? I think this may be a mistake in matplotlib, but then that would not make sense, since it works fine on my computer, and we have the same versions. Any ideas or suggestions?

+4
source share
4 answers

Error starting ipython inline.

 ipython.exe notebook --pylab=inline 

To fix this problem, simply remove the '= inline'.

+1
source

This usually means that no numbers are displayed on the canvas. This also explains why there is no corresponding image when deleting the argument! For instance:

 import pylab pylab.savefig('test', bbox_inches='tight') 

It produces 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)) 
+4
source

I had the same error message. I showed the image via gui and then saved it, which gave an error. I resolved it by first saving it and only then showing it.

+3
source

In my own code, I "solved" this problem by calling savefig in the figure, and not from pyplot ( plt.savefig() ), that is:

 fig.savefig(filename, bbox_inches='tight') 

where fig is an instance of matplotlib.figure.Figure . For me, this is not a problem because of ipython , but rather because of an attempt to update and draw shapes in a long loop.

+1
source

All Articles