2

I'm wanting to mimic the figure extents observed in an output figure and apply them to the figure object itself. The output figure command I want to copy is:

plt.savefig(flname, bbox_inches='tight', pad_inches=0.03)


I've been able to grab the bounding box which generates the observed bbox in the figure using:

bbox = fig.get_tightbbox(fig.canvas.get_renderer())

but am lost as to how to apply that to the fig object!

wblack
  • 343
  • 1
  • 2
  • 11

2 Answers2

0

If you go here:

http://matplotlib.org/api/figure_api.html

And look under the Figure class constructor you will find that in add_axes() and gca() there is a way to set the bbox using one of the kwargs, clip_box.

Additionally here is more information about the bbox.

http://stackoverflow.com/questions/29809238/definition-of-matplotlib-pyplot-axes-bbox

I hope this helps you like it did for me. In short, you cannot apply it to a figure, but you can seem to apply it to all axes.

jimh
  • 1,651
  • 2
  • 15
  • 28
0

If I read this question correctly, it hasn't quite been answered by dozens of answers on this general topic elsewhere on stack overflow. The overwhelming focus there actually is on savefig output, rather than on the figure handle in memory. My application was also on the figure handle, and it worked easily enough like this:

# make figure. w,h and dpi optional but allow for exact figure sizing
hf = plt.plot(x,y,figsize=(w,h),dpi=myscreendpi)
# adjust padding as required
hf.set_tight_layout({'pad':0.5})
tightbbox = hf.get_tightbbox(hf.canvas.get_renderer())
# just set it directly to figure size
hf.set(figheight=tightbbox.height,figwidth=tightbbox.width) 
J B
  • 348
  • 1
  • 6