October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan 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 Subplots to PDF in Python

Save an entire Matplotlib Figure to export its subplots together, or use PdfPages to put separate Figures on different pages of one PDF.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For subplots that should appear together on one PDF page, save the Matplotlib Figure once with fig.savefig("subplots.pdf"). For a PDF with separate pages, use PdfPages and save one Figure per page.

Save all subplots together on one PDF page

plt.subplots() returns a Figure and its Axes. The Figure contains the complete subplot arrangement, so call savefig on the Figure—not on each Axes—to export the panels together. PDF is supported; Matplotlib’s documented default save format is PNG, so use a filename ending in .pdf or explicitly set format="pdf". See the Matplotlib savefig documentation.

import matplotlib.pyplot as plt

fig, axs = plt.subplots(2, 2, figsize=(8, 6))
axs[0, 0].plot([1, 2, 3], [1, 4, 9])
axs[0, 1].plot([1, 2, 3], [1, 2, 3])
axs[1, 0].bar(["A", "B"], [3, 5])
axs[1, 1].scatter([1, 2, 3], [3, 1, 2])

fig.savefig("subplots.pdf", format="pdf", bbox_inches="tight")
plt.close(fig)

bbox_inches="tight" trims the output to the figure’s tight bounding box and can help include labels or titles that extend beyond the default bounds. Because it can change the resulting page dimensions, inspect the PDF if a specific page size matters. It is optional; omit it when you need to preserve the figure’s ordinary page size.

Put multiple Figures on separate pages of one PDF

Use PdfPages when each page should contain a different Figure. A Figure may itself contain several subplots. Each call to pdf.savefig(fig) writes that Figure as a new page, as described in the PdfPages API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

with PdfPages("report.pdf") as pdf:
    for i in range(3):
        fig, axs = plt.subplots(1, 2, figsize=(8, 4))
        axs[0].plot([1, 2, 3], [i, i + 1, i + 2])
        axs[1].bar(["A", "B"], [i + 1, i + 2])
        fig.suptitle(f"Page {i + 1}")
        pdf.savefig(fig, bbox_inches="tight")
        plt.close(fig)

The with context manager closes and finalizes the PDF when the block ends, including when control exits because of an exception. Close each saved Figure in a loop to release pyplot-managed figures. You can also call pdf.savefig() to save the active Figure; passing fig explicitly makes clear which Figure is intended. The official multipage PDF example shows this workflow.

Choose the PDF structure that matches your output

Output Use Result
One page containing several subplot panels fig.savefig("subplots.pdf") One Figure, including its Axes, saved together on a page.
One PDF with several pages PdfPages and repeated pdf.savefig(fig) calls Each saved Figure becomes a separate page; each Figure can contain multiple subplots.

If the panels belong together as a single composite chart, use one Figure and save it once. If each page needs its own chart, title, or layout, create a Figure for each page and add it to the same PdfPages object.

Fix spacing and check the exported page

When titles, labels, or panels feel crowded, adjust the subplot margins and gaps on the Figure with fig.subplots_adjust(...). Matplotlib documents this method for controlling subplot spacing in the Figure.subplots_adjust reference.

fig.subplots_adjust(left=0.12, right=0.95, bottom=0.12, top=0.88,
                    wspace=0.35, hspace=0.4)

Increase the Figure size or adjust font sizes if labels remain difficult to read, then open the saved PDF and check that text is legible and nothing is clipped. Use bbox_inches="tight" only if trimming the bounding box is appropriate for your desired page dimensions.

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.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Add optional PDF metadata

Metadata does not affect whether plots are included. If useful for a report, PdfPages accepts a metadata dictionary with fields such as Title, Author, Subject, Keywords, CreationDate, and ModDate; the supported API and an example are documented in the PdfPages reference and multipage PDF example.

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.

Leave a Reply

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

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
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

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.