October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Save Matplotlib Plots as PNG Images in Python

Use Figure.savefig("plot.png") to export a Matplotlib chart as a PNG, with optional controls for resolution, whitespace and transparency.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.