- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Saving multiple figures to one PDF file in matplotlib
To save multiple figures in one PDF file at once, we can take follwong steps
Steps
Set the figure size and adjust the padding between and around the subplots.
Create a new figure (fig1) or activate and existing figure using figure() method.
Plot the first line using plot() method.
Create another figure (fig2) or activate and existing figure using figure() method.
Plot the second line using plot() method.
Initialize a variable, filename, to make a pdf file.
Create a user-defined function save_multi_image() to save multiple images in a PDF file.
Call the save_multi_image() function with filename.
Create a new PdfPages object.
Get the number of open figures.
Iterate the opened figures and save them into the file.
Close the created PDF object.
Example
from matplotlib import pyplot as plt from matplotlib.backends.backend_pdf import PdfPages plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig1 = plt.figure() plt.plot([2, 1, 7, 1, 2], color='red', lw=5) fig2 = plt.figure() plt.plot([3, 5, 1, 5, 3], color='green', lw=5) def save_multi_image(filename): pp = PdfPages(filename) fig_nums = plt.get_fignums() figs = [plt.figure(n) for n in fig_nums] for fig in figs: fig.savefig(pp, format='pdf') pp.close() filename = "multi.pdf" save_multi_image(filename)
Output
Upon execution, it will create a PDF "multi.pdf" in the Project Directory and save the following two images in that file.
- Related Articles
- Saving all the open Matplotlib figures in one file at once
- How to save figures to pdf as raster images in Matplotlib?
- Drawing multiple figures in parallel in Python with Matplotlib
- Saving a 3D-plot in a PDF 3D with Python
- Saving scatterplot animations with matplotlib
- How to create a pdf file in Java?
- How to show multiple images in one figure in Matplotlib?
- The following figures have more than one line of symmetry. Such figures are said to have multiple lines of symmetry.Identify multiple lines of symmetry\n
- Flushing all current figures in matplotlib
- How to customize spines of Matplotlib figures?
- How to show two figures using Matplotlib?
- How to display multiple images in one figure correctly in matplotlib?
- How to plot multiple horizontal bars in one chart with matplotlib?
- Get the list of figures in Matplotlib
- How to use multiple font sizes in one label in Python Matplotlib?
