Use savefig() in Python to export a Matplotlib figure before displaying it. The basic pattern is to create a figure, draw the data, call plt.savefig(), and then call plt.show(). Saving first avoids blank output caused by display backends that clear or close the current figure.
The filename controls the usual output format, while options such as dpi, figsize, bbox_inches, and transparent control how the exported file is rendered. This gives you a reliable way to save a figure in Python for reports, web pages, or later editing.
How do you use plt.savefig() in Python before display with pyplot or Figure?
Create the figure explicitly and save it before show(). This pyplot example writes a PNG file to the current working directory:
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(6, 4))
ax.plot([1, 2, 3], [2, 4, 3])
ax.set_title(“Example figure”)
plt.savefig(“example.png”, dpi=300, bbox_inches=”tight”)
plt.show()
When working with multiple charts, prefer the Figure object. Its savefig() method saves that specific figure instead of whichever plot pyplot currently considers active:
fig.savefig(“reports/line-chart.svg”, bbox_inches=”tight”)
Create the destination directory first if it does not exist. The save operation can accept a string path or a path-like object such as pathlib.Path.
How does savefig() in Python choose extensions, formats, and resolution?
If you omit format, Matplotlib usually infers the format from the filename extension. For example, chart.png creates a PNG, chart.svg creates an SVG, and chart.pdf creates a PDF. You can also specify the format explicitly:
fig.savefig(“chart-output”, format=”png”, dpi=300)
Use PNG for web graphics and other raster images. Use SVG for diagrams that must remain sharp at different display sizes, and PDF for print-oriented documents or vector artwork. Matplotlib may also support formats such as JPEG, TIFF, and PS, depending on the installed backend.
dpi sets raster resolution, measured in dots per inch. A value such as 150 works for many previews; 300 is a common print setting. DPI increases pixel density for PNG and similar raster formats. It does not control vector detail in SVG or PDF in the same way, because those formats describe lines and text mathematically. DPI can still affect rasterized elements embedded within a vector file.
How do you save a figure in Python with figsize, transparency, and bbox_inches?
Set figsize when creating the figure. Its values are width and height in inches, not pixels:
fig, ax = plt.subplots(figsize=(8, 5))
At 300 DPI, an 8-by-5-inch PNG is approximately 2,400 by 1,500 pixels before bounding-box adjustments. Change the figure dimensions for the layout you need, then use DPI to control raster density.
Use transparent=True when the background should show through, such as when placing a chart over a colored document or slide:
fig.savefig(“overlay.png”, dpi=300, transparent=True)
Use bbox_inches=”tight” to trim excess whitespace around axes, labels, and titles. It is useful for compact exports, but inspect the result when annotations extend far outside the axes. A tight bounding box can also make the final dimensions differ from the original figsize.
Why is the saved figure blank or clipped, and where is the file?
A blank file usually results from saving after plt.show(), saving a different active figure, or creating the plot on one Figure object and exporting another. Save before display, and use fig.savefig() when the figure identity matters. In scripts that do not need an interactive window, save the file and omit show().
Clipped titles, axis labels, or legends often need bbox_inches=”tight”. You can also reserve layout space when creating the figure:
fig, ax = plt.subplots(figsize=(7, 4), constrained_layout=True)
fig.savefig(“final-chart.png”, dpi=300, bbox_inches=”tight”)
Confirm the output location by printing an absolute path. Relative filenames are resolved from Python’s current working directory, which may differ from your script’s folder:
from pathlib import Path
path = Path(“exports/final-chart.png”)
fig.savefig(path, dpi=300)
print(path.resolve())
