0

I want to remove the padding around a figure and save it with exactly specified dimensions:

fig, ax = plt.subplots(figsize=(10,5))
x = [1,2,3]
ax.plot(x, x)
fig.savefig("test.pdf", bbox_inches='tight', pad_inches=0.01)

Unfortunately, doing it this way reduces the dimensions of the resulting figure (in this case to 8.2 in, 4.1 in). A similar question has been asked here quite a while ago, but does not consider the complete removal of the padding.

smonsays
  • 400
  • 2
  • 17
  • 1
    You need to remove `bbox_inches='tight'` and `pad_inches=0.01`. This will change the figure size. The linked answer tells you to use `fig.tight_layout()` instead, and ***is*** in fact working correctly. – ImportanceOfBeingErnest Mar 07 '20 at 16:28
  • You are right about that, thanks for pointing it out. My main problem however is to completely remove the padding around the figure without a change of dimension which is why I invoke the `pad_inches=0.01` argument. – smonsays Mar 07 '20 at 18:12
  • 1
    You may want to scale the content ***within*** the figure. `fig.tight_layout()` can be used for that, or else `fig.subplots_adjust(...)` with appropriate parameters. – ImportanceOfBeingErnest Mar 07 '20 at 18:32

1 Answers1

0

As pointed out by ImportanceOfBeingErnest, instead of using the bbox_inches and pad_inches arguments in fig.savefig which changes the dimensions of the figure, one can use

fig.tight_layout(pad=0.01)

to scale the content within the figure instead, keeping the figure size fixed.

smonsays
  • 400
  • 2
  • 17