Plots often need to leave the Python session as files for reports, dashboards, papers, and automated jobs. In Matplotlib, the Figure object is the export boundary because it owns the canvas, layout, Axes, labels, and artists that should appear in the saved file.

fig.savefig() writes that figure to raster outputs such as PNG or vector outputs such as PDF and SVG. The filename extension chooses the format in most scripts, while dpi controls raster resolution and layout settings decide whether labels and titles fit inside the canvas.

Save from the directory where the project should keep artifacts, or pass a full Path. After export, check that each target file exists, has nonzero bytes, and opens with the expected plot content before attaching it to a report or batch job.

Steps to save a Matplotlib figure:

  1. Save the figure export script as save_figure.py.
    save_figure.py
    from pathlib import Path
     
    import matplotlib.pyplot as plt
     
     
    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    tickets = [128, 104, 91, 76, 63, 58]
     
    fig, ax = plt.subplots(figsize=(6.4, 3.6), layout="constrained")
    ax.plot(months, tickets, marker="o", linewidth=2.2, color="tab:blue")
    ax.fill_between(months, tickets, [50] * len(tickets), color="tab:blue", alpha=0.12)
    ax.set_title("Support tickets waiting for triage")
    ax.set_xlabel("Month")
    ax.set_ylabel("Open tickets")
    ax.grid(True, alpha=0.25)
     
    outputs = [
        Path("support-ticket-trend.png"),
        Path("support-ticket-trend.pdf"),
        Path("support-ticket-trend.svg"),
    ]
     
    fig.savefig(outputs[0], dpi=160)
    fig.savefig(outputs[1])
    fig.savefig(outputs[2])
    plt.close(fig)
     
    for path in outputs:
        print(f"saved: {path.name} bytes={path.stat().st_size}")

    fig.savefig() infers PNG, PDF, and SVG from these filename extensions. If format is set, Matplotlib uses the supplied filename verbatim, so keep the extension aligned yourself.

  2. Run the script from the directory where the files should be written.
    $ python save_figure.py
    saved: support-ticket-trend.png bytes=49800
    saved: support-ticket-trend.pdf bytes=14852
    saved: support-ticket-trend.svg bytes=34262

    Byte counts can change with Matplotlib, fonts, backend, metadata, or dpi differences. A nonzero byte count for each requested filename confirms that the files were written.

  3. Open support-ticket-trend.png and check the saved figure.

    The image should show a blue line falling from Jan through Jun with the title Support tickets waiting for triage, both axis labels, and unclipped tick labels.

  4. Add a tight transparent export before plt.close(fig) only when the image must sit over another background.
    fig.savefig(
        "support-ticket-trend-transparent.png",
        dpi=160,
        transparent=True,
        bbox_inches="tight",
    )

    transparent=True removes the figure and Axes background patches during export, and bbox_inches=“tight” trims the saved area around the visible content.

  5. Remove the practice files when they are not part of the project.
    $ rm save_figure.py support-ticket-trend.png support-ticket-trend.pdf support-ticket-trend.svg