How to Save Matplotlib Subplots to PDF in Python

Working on a Python data analysis project where I needed to create multiple visualizations for a client report. Each figure had several subplots showing different aspects of the data. The challenge was to save all these subplots in a single, high-quality PDF file for easy sharing and printing.

If you’ve ever used Matplotlib in Python, you might already know that saving a single plot is easy with plt.savefig(). But when it comes to saving multiple subplots or multiple figures into one PDF, things get a bit tricky.

In this tutorial, I’ll show you exactly how to save subplots to a PDF file in Python using Matplotlib. I’ll cover two simple methods, one using savefig() and another using PdfPages. Both are reliable and easy to implement.

Method 1 – Save Matplotlib Subplots to a PDF Using savefig()

When I first started working with Matplotlib, the simplest way I found to save subplots to a PDF was by using the built-in savefig() function. It’s quick, easy, and works great when you’re dealing with a single figure that contains multiple subplots.

Here’s how you can do it step-by-step.

Step 1: Import the Required Python Libraries

Before creating any plots, we first need to import the required Python libraries. In this case, we’ll use matplotlib.pyplot and numpy for generating sample data.

import matplotlib.pyplot as plt
import numpy as np

I always prefer keeping imports at the top of my script; it keeps things organized and easy to maintain.

Step 2: Create Subplots in Python Using Matplotlib

Now, let’s create a few subplots. Suppose we want to visualize U.S. sales data for four different product categories, Technology, Furniture, Office Supplies, and Grocery, over 12 months.

# Sample data
months = np.arange(1, 13)
tech_sales = np.random.randint(2000, 5000, 12)
furniture_sales = np.random.randint(1500, 4000, 12)
office_sales = np.random.randint(1000, 3000, 12)
grocery_sales = np.random.randint(2500, 6000, 12)

# Create subplots
fig, axs = plt.subplots(2, 2, figsize=(10, 8))

# Plot each category
axs[0, 0].plot(months, tech_sales, color='blue', marker='o')
axs[0, 0].set_title('Technology Sales')

axs[0, 1].plot(months, furniture_sales, color='green', marker='o')
axs[0, 1].set_title('Furniture Sales')

axs[1, 0].plot(months, office_sales, color='orange', marker='o')
axs[1, 0].set_title('Office Supplies Sales')

axs[1, 1].plot(months, grocery_sales, color='red', marker='o')
axs[1, 1].set_title('Grocery Sales')

# Add common labels
for ax in axs.flat:
    ax.set_xlabel('Month')
    ax.set_ylabel('Sales ($)')
    ax.grid(True)

plt.tight_layout()

This code creates a 2×2 grid of subplots, each showing monthly sales for a product category. I used tight_layout() to ensure the subplots don’t overlap.

Step 3: Save the Subplots as a PDF

Once the subplots are ready, we can save them to a PDF file using the savefig() function.

# Save the figure to a PDF file
plt.savefig("us_sales_report.pdf", format='pdf')

# Show the plot
plt.show()

You can see the output in the screenshot below.

Save Matplotlib Subplots to PDF Python

That’s it! You now have a clean, professional-looking PDF file named us_sales_report.pdf saved in your working directory.

This method is perfect when you only need to save one figure (even if it has multiple subplots). However, if you want to save multiple figures in one PDF file, you’ll need a slightly different approach; that’s where the PdfPages class comes in handy.

Method 2 – Save Multiple Matplotlib Subplots to a Single PDF Using PdfPages

When I started automating report generation for clients, I often needed to include several figures in one PDF document. Manually saving each figure and combining them later was time-consuming. That’s when I discovered the PdfPages class from matplotlib.backends.backend_pdf.

This method allows you to save multiple figures, each containing subplots, into a single multi-page PDF file.

Step 1: Import PdfPages

We’ll start by importing PdfPages from the Matplotlib backend.

from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import numpy as np

This import gives us access to the PdfPages class that helps manage multiple pages in a PDF.

Step 2: Create Multiple Figures with Subplots

Next, let’s create a few figures with subplots. Suppose we’re analyzing different regions in the U.S., East, West, Central, and South, and we want each region’s data on a separate page of the PDF.

# Create a PdfPages object
pdf = PdfPages("us_region_sales_report.pdf")

# Define regions and generate random data
regions = ['East', 'West', 'Central', 'South']

for region in regions:
    months = np.arange(1, 13)
    sales = np.random.randint(2000, 7000, 12)
    profits = np.random.randint(500, 2000, 12)

    # Create a figure with two subplots
    fig, axs = plt.subplots(1, 2, figsize=(10, 4))

    # Plot sales data
    axs[0].bar(months, sales, color='skyblue')
    axs[0].set_title(f"{region} Region - Monthly Sales")
    axs[0].set_xlabel("Month")
    axs[0].set_ylabel("Sales ($)")
    axs[0].grid(True)

    # Plot profit data
    axs[1].plot(months, profits, color='green', marker='o')
    axs[1].set_title(f"{region} Region - Monthly Profit")
    axs[1].set_xlabel("Month")
    axs[1].set_ylabel("Profit ($)")
    axs[1].grid(True)

    plt.tight_layout()

    # Save the current figure to the PDF
    pdf.savefig(fig)

    # Close the figure to free memory
    plt.close(fig)

# Close the PDF file
pdf.close()

This Python code creates four separate figures (one for each region) and saves them into a single PDF file named us_region_sales_report.pdf. Each figure contains two subplots, one for sales and one for profit.

Step 3: Verify the PDF Output

After running the code, open the generated file, us_region_sales_report.pdf. You’ll see four pages, each representing a region’s sales and profit data.

You can see the output in the screenshot below.

Save Python Matplotlib Subplots to PDF

This approach is incredibly useful for generating automated reports, especially in data analytics and business intelligence workflows.

Additional Tips for Saving Subplots as PDF in Python

Over the years, I’ve discovered a few best practices that can make your PDF exports look more professional and consistent.

  1. Use High DPI for Better Quality
    Add dpi=300 in savefig() or pdf.savefig() for print-quality output.
   plt.savefig("chart.pdf", format="pdf", dpi=300)
  1. Add Metadata to PDF Files
    You can include metadata such as the author, title, and subject.
   pdf = PdfPages('report.pdf')
   pdf.infodict()['Title'] = 'U.S. Sales Report'
   pdf.infodict()['Author'] = 'John Doe'
   pdf.infodict()['Subject'] = 'Regional Sales Analysis using Python'
  1. Combine with Pandas and Seaborn
    If you’re using Pandas DataFrames, you can integrate Seaborn for more aesthetic plots and still save them using the same approach.
  2. Automate Reports with Loops
    Automating the generation of multiple pages in a PDF is one of the most powerful features of PdfPages. You can loop through datasets, time periods, or product categories and generate professional reports in seconds.

Common Mistakes to Avoid

While saving subplots to PDF in Python is easy, here are some pitfalls I’ve seen developers run into:

  • Forgetting to close figures: Always call plt.close() after saving each figure to avoid memory issues.
  • Overlapping subplots: Use plt.tight_layout() to ensure subplots don’t overlap.
  • Incorrect file paths: Always check your working directory or specify a full path for the PDF file.
  • Large file sizes: If your PDF becomes too large, consider reducing the DPI or simplifying your visuals.

Why I Prefer PDF for Matplotlib Reports

PDFs are ideal for business reporting because they preserve layout, resolution, and color accuracy across devices. When I send visual reports to clients or colleagues in the U.S., I know they’ll see exactly what I intended, no formatting issues, no scaling problems.

Matplotlib’s PDF export features make it easy to create professional, print-ready reports directly from Python scripts.

So that’s how you can save Matplotlib subplots to a PDF in Python, either as a single figure or as multiple pages using PdfPages.

The first method using savefig() is perfect for quick tasks, while the second method PdfPages is ideal for multi-page reports. Both methods are reliable and widely used in Python data visualization workflows.

Other Python articles you may also like:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.