Massyanya
Massyanya

Reputation: 2934

Matplotlib: how to increase the area saved without increasing the figure size

Currently the area of the figure that gets saved by is fixed, so if I have the legend outside the figure, it gets cropped off, like that:

enter image description here

Is there a function in matplotlib that allows to save larger area than the standard area of the figure, so I can save my figure with the legend, like below?

enter image description here

Upvotes: 2

Views: 386

Answers (1)

Mike Müller
Mike Müller

Reputation: 85432

bbox_inches='tight' should do the trick:

from matplotlib import pyplot as plt

plt.savefig('figure.png', bbox_inches='tight')

bbox_inches:

Bbox in inches. Only the given portion of the figure is saved. If 'tight', try to figure out the tight bbox of the figure.

Example

plt.plot(range(10))
plt.legend(['abc'], loc='upper right', bbox_to_anchor=(1.2, 0.9))

results in this png:

enter image description here

Upvotes: 2

Related Questions