Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Pandas
  • Numpy
  • Seaborn
  • Ploty
  • Data visualization
  • Data Analysis
  • Power BI
  • Tableau
  • Machine Learning
  • Deep Learning
  • NLP
  • Computer Vision
  • Data Science for Beginners
  • Data Science interview questions
  • Data analysis interview questions
  • NLP Interview questions
Open In App
Next Article:
Save multiple matplotlib figures in single PDF file using Python
Next article icon

Save multiple matplotlib figures in single PDF file using Python

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Installation

pip install matplotlib

Stepwise Implementation

To come up with a solution, we will follow a few steps.

Step 1: Import necessary files.

Python3
from matplotlib import pyplot as plt from matplotlib.backends.backend_pdf import PdfPages 

Step 2: Set up the figure size and adjust the padding between and around the subplots.

Python3
plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True 

Step 3: We will consider 3 plots, so let's name them fig1, fig2, and fig3 using plt.figure().

Python3
fig1 = plt.figure() fig2 = plt.figure() Fig3 = plt.figure() 

Step 4: Plot the first line using the plt.plot() method.

Python3
plt.plot([17, 45, 7, 8, 7], color='orange') plt.plot([13, 25, 1, 6, 3], color='blue') plt.plot([22, 11, 2, 1, 23], color='green') 

Step 5: Create a function to save multiple images in a PDF file let's say save_image().

Python3
def save_image(filename):        # PdfPages is a wrapper around pdf      # file so there is no clash and     # create files with no error.     p = PdfPages(filename)          # get_fignums Return list of existing     # figure numbers     fig_nums = plt.get_fignums()       figs = [plt.figure(n) for n in fig_nums]          # iterating over the numbers in list     for fig in figs:                 # and saving the files         fig.savefig(p, format='pdf')               # close the object     p.close()   

Complete Code 

Python3
import matplotlib from matplotlib import pyplot as plt from matplotlib.backends.backend_pdf import PdfPages  # customizing runtime configuration stored # in matplotlib.rcParams plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True  fig1 = plt.figure() plt.plot([17, 45, 7, 8, 7], color='orange')  fig2 = plt.figure() plt.plot([13, 25, 1, 6, 3], color='blue')  Fig3 = plt.figure() plt.plot([22, 11, 2, 1, 23], color='green')   def save_image(filename):        # PdfPages is a wrapper around pdf      # file so there is no clash and create     # files with no error.     p = PdfPages(filename)          # get_fignums Return list of existing      # figure numbers     fig_nums = plt.get_fignums()       figs = [plt.figure(n) for n in fig_nums]          # iterating over the numbers in list     for fig in figs:                 # and saving the files         fig.savefig(p, format='pdf')           # close the object     p.close()    # name your Pdf file filename = "multi_plot_image.pdf"    # call the function save_image(filename)   

Output:

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".

 
 
 

Next Article
Save multiple matplotlib figures in single PDF file using Python

D

devgarg05
Improve
Article Tags :
  • Technical Scripter
  • Python
  • Technical Scripter 2022
  • Python-matplotlib
Practice Tags :
  • python

Similar Reads

    Save Matplotlib Figure as SVG and PDF using Python
    In this article, we will see how can we save the Matplotlib figure as Scalable Vector Graphics(SVG) using Python or any other file format for further use. The required modules for this tutorial are Matplotlib. Matplotlib is a comprehensive library for creating static, animated, and interactive visua
    3 min read
    Matplotlib.figure.Figure.savefig() in Python
    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
    2 min read
    Multiplots in Python using Matplotlib
    Matplotlib is a Python library that can be used for plotting graphs and figures. Plotting multiplots or multiple plots are often required either for comparing the two curves or show some gradual changes in the multiple plots, and this can be done using Subplots. Subplots are one of the most importan
    3 min read
    Matplotlib.figure.Figure.set_size_inches() in Python
    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
    2 min read
    Matplotlib.figure.Figure.suptitle() in Python
    Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences