Save a Matplotlib figure as a PNG with fig.savefig("plot.png"). The filename extension tells Matplotlib which format to write; add options such as dpi, bbox_inches or transparent when you need control over resolution, margins or background.
Save a figure as a PNG
Use the Figure.savefig method to export the specific figure you created:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [2, 4, 3])
fig.savefig("plot.png")
Matplotlib writes plot.png to the current working directory. To choose another location, pass a relative or absolute path, such as fig.savefig("images/plot.png"). The destination can also be a binary file-like object. See the Figure.savefig API and the figures guide.
For a quick script that uses pyplot’s current figure, plt.savefig("plot.png") also works. Prefer fig.savefig(...) in reusable code or when you may have multiple figures: it makes the export target explicit.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
Choose the format and filename deliberately
When you omit the format argument, Matplotlib infers the format from the filename extension. Thus, fig.savefig("plot.png") saves PNG. If the filename has no extension, Matplotlib uses the configured savefig.format setting and appends that extension.
If you set format explicitly, Matplotlib uses the filename exactly as given and does not change or append an extension to match it. Keep the two consistent—for example, use fig.savefig("plot.png", format="png") if you specify the format.
Rank #2
Set resolution with DPI
The dpi argument sets raster resolution in dots per inch. The saved pixel dimensions depend on both the figure’s size in inches and the output DPI: increasing DPI produces more pixels for the same-sized figure, while changing the figure size also affects the result.
fig.savefig("plot.png", dpi=300)
Choose a DPI that suits the intended display or print use; there is no universally correct value. If you omit it, the default is "figure", which uses the Figure’s DPI. Matplotlib’s savefig API documents the parameter, and its figures guide shows saving with dpi=200.
Trim whitespace around the plot
To crop the saved image to the bounding box around the figure’s artists, use bbox_inches="tight". Set pad_inches to adjust the margin around that crop:
fig.savefig("plot.png", bbox_inches="tight", pad_inches=0.1)
pad_inches is measured in inches and defaults to 0.1 in the documented configuration. Tight cropping can change the saved image’s dimensions, so use it when you want to remove surrounding margins rather than preserve a fixed canvas size.
Save with a transparent or colored background
For a plot that will sit on top of another background, make the figure and axes patches transparent:
fig.savefig("plot.png", transparent=True)
Transparency is off by default. With transparent=True, Matplotlib makes the Axes patches transparent and also the Figure patch unless you specify facecolor or edgecolor. To choose saved figure colors instead, use those arguments; "auto" uses the Figure’s current colors.
Best Value
Know which Matplotlib export method to use
Use Figure.savefig (or pyplot’s savefig) for a chart built from axes, labels and plotted artists. Use matplotlib.image.imsave when you have image-array data to write, such as a two-dimensional grayscale-shaped array or a three-dimensional RGB or RGBA array. For a single-channel array that must be stored as grayscale, the imsave documentation recommends an image I/O library such as Pillow, tifffile or imageio. For imsave, DPI is stored as metadata and does not change the output resolution.
Relevant savefig defaults
Matplotlib’s current stable configuration documentation lists these defaults. They are configurable settings, not fixed behavior:
| Setting | Default | Effect |
|---|---|---|
savefig.format |
png |
Format used when the filename has no extension. |
savefig.dpi |
figure |
Uses the Figure’s DPI unless another value is selected. |
savefig.bbox |
None |
Does not request a tight bounding box by default. |
savefig.pad_inches |
0.1 |
Padding in inches when a tight bounding box is used. |
savefig.transparent |
False |
Does not make the figure transparent by default. |
These defaults are listed in Matplotlib’s configuration reference.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitches




