Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with bbox_inches='tight' in matplotlib

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?

like image 508
Osmond Bishop Avatar asked Jan 13 '23 13:01

Osmond Bishop


2 Answers

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))
like image 68
Hooked Avatar answered Jan 16 '23 04:01

Hooked


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.

like image 24
someguy Avatar answered Jan 16 '23 04:01

someguy