Note that there are some explanatory texts on larger screens.

plurals
  1. POMatplotlib: make final figure dimensions match figsize with savefig() and bbox_extra_artists
    primarykey
    data
    text
    <p>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.</p> <p>I need to use the <code>bbox_extra_artists</code> argument to <code>savefig</code> 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 <code>figsize</code> when creating the plot honoured after calling <code>savefig</code> with <code>bbox_extra_artists</code>.</p> <p>My call to <code>savefig</code> looks like this:</p> <pre><code>savefig(output_file, bbox_inches='tight', pad_inches=0.0,dpi=72.27,bbox_extra_artists=(lgd,tp,ur,hrs)) </code></pre> <p>The figure width I specify with <code>figsize</code> is:</p> <pre><code>516.0 * (1/72.27) = 7.1398 inches = 181.3532 millimeters </code></pre> <p>The output PDF width I get using my <code>savefig()</code> call above is <code>171 millimeters</code> (not the desired 181.3532 millimeters).</p> <p>The solution I've seen proposed in other questions here on SO is to make a call to <code>tight_layout()</code>. So, immediately above my <code>savefig()</code> call, I put the following:</p> <pre><code>plt.tight_layout(pad=0.0,h_pad=0.0,w_pad=0.0) </code></pre> <p>This produces a figure with width <code>183 millimeters</code> (again, not the 181.3532 millimeters I want). If I use <code>tight_layout</code>, and remove the <code>bbox_extra_artists</code> argument from my call to <code>savefig()</code>, I get a width of <code>190 millimeters</code> (again, not 181.3532 millimeters that I want). This is besides the point that removing <code>bbox_extra_artists</code> in my case mangles up the figure by cutting things off.</p> <p>So I guess this is a two part question:</p> <ul> <li>When using <code>tight_layout</code>, even without <code>bbox_extra_artists</code>, why is the output figure incorrectly sized?</li> <li>Is there <em>any</em> way to get a correctly sized figure when using <code>bbox_extra_artists</code>?</li> </ul> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload