Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: make final figure dimensions match figsize with savefig() and bbox_extra_artists

I'm producing graphics for publication with matplotlib and want very precisely sized figure output. I need this so that I can be sure the figure won't need to be resized when inserted into the latex document, which would mess with the font size in the figure which I want to keep at a consistent ratio to the font size in the main document.

I need to use the bbox_extra_artists argument to savefig because I have a legend down the bottom that gets cut off the figure if I don't. The problem I am having is that I haven't found a way to have the original figure dimensions I specify with figsize when creating the plot honoured after calling savefig with bbox_extra_artists.

My call to savefig looks like this:

savefig(output_file, bbox_inches='tight', pad_inches=0.0,dpi=72.27,bbox_extra_artists=(lgd,tp,ur,hrs))

The figure width I specify with figsize is:

516.0 * (1/72.27) = 7.1398 inches = 181.3532 millimeters 

The output PDF width I get using my savefig() call above is 171 millimeters (not the desired 181.3532 millimeters).

The solution I've seen proposed in other questions here on SO is to make a call to tight_layout(). So, immediately above my savefig() call, I put the following:

plt.tight_layout(pad=0.0,h_pad=0.0,w_pad=0.0)

This produces a figure with width 183 millimeters (again, not the 181.3532 millimeters I want). If I use tight_layout, and remove the bbox_extra_artists argument from my call to savefig(), I get a width of 190 millimeters (again, not 181.3532 millimeters that I want). This is besides the point that removing bbox_extra_artists in my case mangles up the figure by cutting things off.

So I guess this is a two part question:

  • When using tight_layout, even without bbox_extra_artists, why is the output figure incorrectly sized?
  • Is there any way to get a correctly sized figure when using bbox_extra_artists?

I know a few millimeters sounds like a trivial difference, but it's the fact that there is any difference at all that concerns me. It means there is some variable, which could change in my other figures which is causing a degree of error, and that error may well be magnified elsewhere.

like image 629
Bryce Thomas Avatar asked Apr 20 '13 08:04

Bryce Thomas


People also ask

Does PLT Savefig overwrite?

It will create the figure the first time the code is run. If I make a change to the code (e.g. change label) and then re-run, the plot shown in a Jupyter Notebook is updated, but the file saved on my machine is not!

How do I fix the canvas size in matplotlib?

To set the size of the plotting canvas in matplotlib, we can take the following steps: Set the figure size and adjust the padding between and around the subplots. Use figsize 7.50 and 3.50 to set the figure size. Create x and y data points using numpy.

What does PLT Tight_layout () do?

The tight_layout() function in pyplot module of matplotlib library is used to automatically adjust subplot parameters to give specified padding.


1 Answers

The reason that you're getting a smaller plot is because you're specifying bbox_inches='tight'.

bbox_inches='tight' crops the plot down based on the extents of the artists in the plot. If you want the output to be exactly the size you specify, then just leave out the bbox_inches and bbox_extra_artists kwargs entirely.

If you just do savefig(output_file, dpi=72.72) without anything else the plot will be exactly the size you specified with creating the figure.

like image 161
Joe Kington Avatar answered Oct 14 '22 13:10

Joe Kington