Home > database >  How to save figure in matplotlib ajusted to the object size?
How to save figure in matplotlib ajusted to the object size?

Time:11-02

Hello everyone I am trying to save the figures from matplotlib. It saves with blank spaces around the figure, I mean It adds space, It is no saved with ajusted object size. It is very easy to edit them in Inkscape (I make that in this software), but that's not nice when there are more than 50 figures. I do that because in beamer slides a figure with extra space occupies a lot of space, because of the sheet it is very small.

enter image description here

CodePudding user response:

Use constrained layout in:

plt.figure function

# 3.5.3 and 3.6
fig = plt.figure(figsize=(8, 6), layout="constrained")
fig.add_subplot()
plt.plot([1, 2], [3, 4])

plt.subplots function

# 3.5.3 and 3.6
fig, ax = plt.subplots(figsize=(8, 6), layout="constrained")
ax.plot([1, 2], [3, 4])

Set layout on Figure object, after its creation:

# 3.5.3
fig.set_constrained_layout(True)

# 3.6
fig.set_layout_engine("constrained")

More info:

https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html#sphx-glr-tutorials-intermediate-constrainedlayout-guide-py

  • Related