Save Matplotlib Plots as PDF in Python

When working with data visualization in Python, Matplotlib is my go-to library. Over the years, I’ve found that saving plots as PDF files is an essential step, especially when preparing reports or presentations for clients, colleagues, or stakeholders.

PDFs maintain vector quality, ensuring your charts look crisp regardless of zoom or print size, something that’s crucial when sharing detailed graphs. In this article, I’ll walk you through the best ways to save Matplotlib plots as PDF files.

Let’s get in!

Methods to Save Matplotlib Plots as PDF

Before getting into the how-to, let me share why I prefer PDFs over other formats like PNG or JPEG:

  • Scalability: PDFs are vector-based, so graphics don’t pixelate when zoomed.
  • Professionalism: PDFs are widely accepted in corporate and academic environments.
  • File Size: PDFs often have smaller file sizes than high-resolution images.
  • Compatibility: PDFs can be easily embedded into documents or presentations.

Now, let’s explore how you can save your plots as PDFs.

1: Use savefig() to Save Directly as PDF

The simplest and most common way I use to save a Matplotlib figure as a PDF is the savefig() function in Python. It supports various formats, including PDF, by specifying the file extension.

Here’s how you do it:

import matplotlib.pyplot as plt

# Sample data: Monthly sales in USD for a retail company in New York
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [25000, 27000, 30000, 28000, 32000, 31000]

plt.plot(months, sales, marker='o')
plt.title('Monthly Sales in New York Store')
plt.xlabel('Month')
plt.ylabel('Sales (USD)')
plt.grid(True)

# Save the plot as a PDF file
plt.savefig('monthly_sales_ny.pdf')

plt.show()

You can see the output in the screenshot below.

matplotlib save as pdf

What’s happening here?

  • After plotting, plt.savefig('filename.pdf') saves the plot in PDF format.
  • You can specify the path if you want to save it elsewhere.
  • The saved PDF maintains vector quality, perfect for zooming or printing.

Read Put the Legend Outside the Plot in Matplotlib

2: Set DPI and Transparent Background in PDF

Sometimes, you may want to control the resolution or background of your saved PDF. Although vector graphics don’t lose quality with DPI, setting it can affect embedded raster elements like images.

Also, setting a transparent background can be useful if you plan to overlay the plot on other media.

Example:

plt.plot(months, sales, marker='o')
plt.title('Monthly Sales in New York Store')
plt.xlabel('Month')
plt.ylabel('Sales (USD)')
plt.grid(True)

# Save as PDF with 300 dpi and transparent background
plt.savefig('monthly_sales_ny_transparent.pdf', dpi=300, transparent=True)

plt.show()

You can see the output in the screenshot below.

matplotlib savefig pdf

This method is handy when preparing visuals for presentations where you might want the background to blend seamlessly.

3: Use PdfPages to Save Multiple Plots in a Single PDF

When working on reports, I often need to save multiple plots in one PDF file. Matplotlib’s PdfPages class from matplotlib.backends.backend_pdf makes this easy.

Here’s an example of saving two plots in a single PDF:

from matplotlib.backends.backend_pdf import PdfPages

with PdfPages('sales_report_ny.pdf') as pdf:
    # First plot
    plt.figure()
    plt.plot(months, sales, marker='o')
    plt.title('Monthly Sales in New York Store')
    plt.xlabel('Month')
    plt.ylabel('Sales (USD)')
    plt.grid(True)
    pdf.savefig()  # saves the current figure into the PDF
    plt.close()

    # Second plot: Cumulative sales
    cumulative_sales = [sum(sales[:i+1]) for i in range(len(sales))]
    plt.figure()
    plt.plot(months, cumulative_sales, marker='x', color='green')
    plt.title('Cumulative Sales in New York Store')
    plt.xlabel('Month')
    plt.ylabel('Cumulative Sales (USD)')
    plt.grid(True)
    pdf.savefig()
    plt.close()
  • Organizes multiple plots into one document.
  • Keeps your reports neat and consolidated.
  • Easy to automate when generating many plots programmatically.

Check out Matplotlib Invert y axis

4: Save Plots as PDF with Matplotlib Object-Oriented API

If you prefer the object-oriented approach (which I recommend for larger projects), saving PDFs works similarly.

Example:

fig, ax = plt.subplots()

ax.plot(months, sales, marker='o')
ax.set_title('Monthly Sales in New York Store')
ax.set_xlabel('Month')
ax.set_ylabel('Sales (USD)')
ax.grid(True)

fig.savefig('monthly_sales_ny_oop.pdf')
plt.close(fig)

This approach gives you more control over figure and axes properties and is easier to maintain in complex scripts.

Additional Tips for Saving PDFs with Matplotlib

  • File Naming: Use descriptive filenames like sales_jan_to_jun_2025.pdf for better organization.
  • File Path: You can specify full or relative paths in savefig(), e.g., plt.savefig('reports/2025/sales.pdf').
  • \Fonts and Styles: Customize fonts and styles before saving to match your branding or report style.
  • Interactive Plots: Note that saving to PDF is static; interactive elements (like zoom or tooltips) won’t work in PDFs.

Saving Matplotlib plots as PDFs is a simple yet useful way to share your visualizations with high quality and professionalism. Whether you need a quick single-plot export or a multi-page report, Matplotlib offers flexible options to suit your needs.

I encourage you to try these methods in your projects. Once you get comfortable, saving your plots as PDFs will become a seamless part of your data visualization workflow.

You may also read other articles related to Matplotlib:

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.