Skip to content
LearnPY
Matplotlibfunction reference

Figure.savefig

Write a figure to an image file.

Signature

Figure.savefig(fname, dpi='figure', bbox_inches=None)

What it does

savefig renders the current figure to a path. The extension chooses the format. bbox_inches="tight" trims surrounding whitespace, which is often useful for reports and web pages.

Parameters

fnamestr or path-like
Destination path, including the file extension.
dpinumber or 'figure', optionaloptional
Resolution in dots per inch.
bbox_inchesstr or Bbox, optionaloptional
Use 'tight' to trim extra whitespace.

Returns

None

Example

pythonFigure.savefig example
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(4, 3))
ax.plot([1, 2, 3], [1, 4, 2])
fig.savefig("readings.png", dpi=150, bbox_inches="tight")

Watch out for

  • Save before plt.show in scripts that close figures, or the canvas may already be cleared.
  • A missing extension can cause the wrong format or an error depending on the backend.