Save multiple matplotlib figures in single PDF file using Python
In this article, we will discuss how to save multiple matplotlib figures in a single PDF file using Python. We can use the PdfPages class’s savefig() method to save multiple plots in a single pdf. Matplotlib plots can simply be saved as PDF files with the .pdf extension. This saves Matplotlib-generated figures in a single PDF file named Save multiple plots as PDF.pdf in the current working directory.
pip install matplotlib
To come up with a solution, we will follow a few steps.
Step 1: Import necessary files.
Step 2: Set up the figure size and adjust the padding between and around the subplots.
Step 3: We will consider 3 plots, so let’s name them fig1, fig2, and fig3 using plt.figure().
Step 4: Plot the first line using the plt.plot() method.
Step 5: Create a function to save multiple images in a PDF file let’s say save_image().
Now after you run the code you can see on your local directory that a pdf containing all three plots will be saved in a pdf named “multi_plot_image.pdf”.