For one graph, call fig.savefig("graph.pdf") on its Matplotlib Figure. The .pdf extension tells Matplotlib to write a PDF. Use PdfPages when you want several figures in one multi-page PDF.
Save one Matplotlib graph as a PDF
Create a figure and axes, draw the graph, then save the figure:
As an Amazon Associate I earn from qualifying purchases.
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [2, 4, 3])
ax.set(title="Example graph", xlabel="X", ylabel="Y")
fig.savefig("graph.pdf")
This writes graph.pdf in the program’s current working directory. To save elsewhere, pass a path, such as "reports/graph.pdf"; the directory must exist.
The documented Figure.savefig API infers the output format from a filename extension when you omit format. You can also specify it explicitly:
#1 Best Overall
fig.savefig("graph.pdf", format="pdf")
When you set format="pdf", Matplotlib uses the filename verbatim: it does not add an extension or change one to match the format. Include .pdf in the filename if that is the intended path. A filename without an extension does not imply PDF; the current configuration reference lists PNG as the default save format.
Keep labels from being cropped
If a title, axis label, or legend sits close to the figure edge, request a tight bounding box:
Rank #2
fig.savefig("graph.pdf", bbox_inches="tight")
This asks Matplotlib to save the tight bounding box around the figure. Use pad_inches to adjust the padding around that box; its documented default is 0.1 inches. For example:
Free tools Windows power users keep installed
One-click scans. No signup required.
fig.savefig("graph.pdf", bbox_inches="tight", pad_inches=0.2)
These options are documented in the savefig API and the Matplotlib configuration reference.
Put several figures in one PDF
Use Matplotlib’s PdfPages context manager to make a multi-page PDF. Each call to pdf.savefig(fig) adds the supplied figure as a page:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
with PdfPages("graphs.pdf") as pdf:
for values in ([1, 2, 3], [3, 2, 1]):
fig, ax = plt.subplots()
ax.plot(values)
pdf.savefig(fig, bbox_inches="tight")
plt.close(fig)
The with block closes the PDF when finished. The example closes each figure after adding it to help release resources, which is useful when creating many pages. The PDF backend documentation also describes optional document metadata. Its supported information keys include Title, Author, Subject, Keywords, Creator, and Producer.
Choose the save method by the document you need
- One figure, one PDF: call
fig.savefig("graph.pdf"). - Several figures, several PDF pages: open
PdfPagesand callpdf.savefig(fig)for each figure.
savefig accepts a path-like name, a file-like object, or a backend-dependent object. Which formats are available depends on the active backend; see the API reference for details.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallQuick Recap
Best Value
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.




