Save Matplotlib figures without opening a window

Save a figure to PNG, PDF, or SVG, configure a noninteractive backend, control resolution and transparency, and close figures in batch jobs.

Best practices
Python programming and application logging

Call fig.savefig("plot.png") to save a Matplotlib figure, then close it when you no longer need it. A noninteractive backend such as Agg is useful in a server or batch job where no display window should open.

Create and save a figure

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [2, 4, 6, 8])
ax.set_title("Example chart")
fig.savefig("plot.png")
plt.close(fig)

Choose the backend before importing pyplot and creating figures for a predictable standalone script. Matplotlib can select a noninteractive backend automatically in some environments; manually forcing one is not always required. Notebook display integration has its own behavior.

Set output options

fig, ax = plt.subplots(figsize=(6, 4))
ax.plot([1, 2, 3], [2, 4, 3])
fig.savefig(
    "plot.png",
    dpi=300,
    bbox_inches="tight",
    transparent=True,
)
plt.close(fig)

For raster output, DPI and figure dimensions determine pixel size, although a tight bounding box can change the final dimensions. The default DPI comes from configuration and may follow the figure's DPI; it is not universally 100. A value of 300 is a common export choice, not a requirement for every publication.

The filename extension normally selects the output format. Supported formats depend on the backend. PNG supports transparency; JPEG does not. PDF and SVG can preserve vector elements, while rasterized elements still have a resolution.

Avoid blank output

Save before a blocking plt.show() when using pyplot's current-figure interface. A blocking show can close and unregister figures, so a later plt.savefig() may target a new empty figure. Keeping the explicit fig reference and calling fig.savefig() makes the intended figure clear. Also check that you plotted on that figure and did not clear it before saving.

FAQ

Why are some characters rendered as boxes? The selected font may lack those glyphs. Install and select a font covering the text you need. Font names are platform-dependent, so verify availability rather than assuming a particular font is installed. Check minus-sign rendering with the chosen font too.

Why does a batch job keep growing in memory? Close figures after saving and release references you no longer need. Closing unregisters a figure from pyplot; it does not instantly free objects still referenced elsewhere. Reusing a figure deliberately is also possible when you clear or update its content correctly.

Can I save several formats? Yes. Call fig.savefig() for each destination before discarding the figure, for example chart.png and chart.svg.

References

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting