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


i'm producing graphics publication matplotlib , want precisely sized figure output. need can sure figure won't need resized when inserted latex document, mess font size in figure want keep @ consistent ratio font size in main document.

i need use bbox_extra_artists argument savefig because have legend down bottom gets cut off figure if don't. problem having haven't found way have original figure dimensions specify figsize when creating plot honoured after calling savefig bbox_extra_artists.

my call savefig looks this:

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

the figure width specify figsize is:

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

the output pdf width using savefig() call above 171 millimeters (not desired 181.3532 millimeters).

the solution i've seen proposed in other questions here on make call tight_layout(). so, above savefig() call, put following:

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

this produces figure width 183 millimeters (again, not 181.3532 millimeters want). if use tight_layout, , remove bbox_extra_artists argument call savefig(), width of 190 millimeters (again, not 181.3532 millimeters want). besides point removing bbox_extra_artists in case mangles figure cutting things off.

so guess 2 part question:

  • when using tight_layout, without bbox_extra_artists, why output figure incorrectly sized?
  • is there any way correctly sized figure when using bbox_extra_artists?

i know few millimeters sounds trivial difference, it's fact there difference @ concerns me. means there variable, change in other figures causing degree of error, , error may magnified elsewhere.

the reason you're getting smaller plot because you're specifying bbox_inches='tight'.

bbox_inches='tight' crops plot down based on extents of artists in plot. if want output size specify, leave out bbox_inches , bbox_extra_artists kwargs entirely.

if savefig(output_file, dpi=72.72) without else plot size specified creating figure.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -