Creating a Multipage PDF with Matplotlib

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to create a multipage PDF file with Python Matplotlib. The PDF file will contain several pages with different plots and metadata. You will also learn how to attach annotations to the PDF file.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import Libraries

First, you need to import the necessary libraries for creating the PDF file. In this lab, we will use Matplotlib and datetime libraries.

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

Create the PdfPages Object

Next, you need to create a PdfPages object to which you will save the pages of the PDF file. You can use the 'with' statement to make sure that the PdfPages object is closed properly at the end of the block, even if an exception occurs.

with PdfPages('multipage_pdf.pdf') as pdf:

Create the First Page

In this step, you will create the first page of the PDF file. The page will contain a plot of a simple graph.

plt.figure(figsize=(3, 3))
plt.plot(range(7), [3, 1, 4, 1, 5, 9, 2], 'r-o')
plt.title('Page One')
pdf.savefig()
plt.close()

Create the Second Page

In this step, you will create the second page of the PDF file. The page will contain a plot of a sine wave.

plt.rcParams['text.usetex'] = True
plt.figure(figsize=(8, 6))
x = np.arange(0, 5, 0.1)
plt.plot(x, np.sin(x), 'b-')
plt.title('Page Two')
pdf.attach_note("plot of sin(x)")  ## attach metadata (as pdf note) to page
pdf.savefig()
plt.close()

Create the Third Page

In this step, you will create the third page of the PDF file. The page will contain a plot of a parabola.

plt.rcParams['text.usetex'] = False
fig = plt.figure(figsize=(4, 5))
plt.plot(x, x ** 2, 'ko')
plt.title('Page Three')
pdf.savefig(fig)  ## or you can pass a Figure object to pdf.savefig
plt.close()

Set the Metadata of the PDF File

In this step, you will set the metadata of the PDF file. You can set the title, author, subject, keywords, and creation/modification date of the PDF file.

d = pdf.infodict()
d['Title'] = 'Multipage PDF Example'
d['Author'] = 'Jouni K. Sepp\xe4nen'
d['Subject'] = 'How to create a multipage pdf file and set its metadata'
d['Keywords'] = 'PdfPages multipage keywords author title subject'
d['CreationDate'] = datetime.datetime(2009, 11, 13)
d['ModDate'] = datetime.datetime.today()

Summary

In this lab, you learned how to create a multipage PDF file with Python Matplotlib. You also learned how to attach metadata and annotations to the PDF file. You can use these techniques to create professional reports with multiple plots and annotations.

Other Python Tutorials you may like