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:
tight_layout
, even without bbox_extra_artists
, why is the output figure incorrectly sized?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.
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!
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.
The tight_layout() function in pyplot module of matplotlib library is used to automatically adjust subplot parameters to give specified padding.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With