Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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 Plot To Numpy Array using Matplotlib
Next article icon

How to Save a Plot to a File Using Matplotlib?

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

Matplotlib is a popular Python library to create plots, charts, and graphs of different types. show() is used to plot a plot, but it doesn't save the plot to a file. In this article, we will see various methods through which a Matplotlib plot can be saved as an image file.

Methods to Save a Plot in Matplotlib

There are several ways to save plots in Matplotlib:

  1. Using savefig()
  2. Using matplotlib.pyplot.imsave()
  3. Using the Pillow Library

1. Using savefig() Method

savefig() method is the most popular way of saving plots of Matplotlib. This function enables you to save a plot in the form of a file on your local system in different formats like PNG, JPEG, SVG, etc.

In this example, we are creating our own data list, and using Matplotlib we are plotting a bar graph and saving it to the same directory. To save generated graphs in a file on a storage disk, savefig() method is used.

Python
import matplotlib.pyplot as plt  year = ['2010', '2002', '2004', '2006', '2008'] production = [25, 15, 35, 30, 10]  # Plotting bar chart plt.bar(year, production)  # Saving the plot as a JPEG file plt.savefig("output.jpg")  # Saving the plot with additional parameters (this is optional) plt.savefig("output1.jpg", facecolor='yellow', bbox_inches="tight", pad_inches=0.3, transparent=True) 

Output

saving-plot-matplotlib
plot
plot
plot

2. Using matplotlib.pyplot.imsave()

imsave() function is another method to save a plot as an image file. It’s commonly used to save 2D arrays as image files, making it especially useful for working with image data.

Python
import matplotlib.pyplot as plt import numpy as np  # Generate random image data img = np.random.rand(100, 100)  # Save image plt.imsave('sample_image.png', img, cmap='gray') 

Output

Sample_image

3. Using the Pillow Library (PIL)

In certain instances, it could be helpful to transform a Matplotlib plot into a Pillow (PIL) Image object and save it. This is specifically useful when one is dealing with image processing operations that need Pillow.

  • BytesIO: We use a BytesIO buffer to store the plot image in memory, which can then be processed by PIL.
  • fig.savefig(): Instead of converting the canvas directly to a string, we save the plot to the buffer in PNG format.
  • Image.open(): We load the image from the buffer into a PIL image object.
Python
import matplotlib.pyplot as plt from PIL import Image import io  fig, ax = plt.subplots() ax.plot([1, 2, 3], [1, 2, 3])  # Save the plot to a BytesIO object buf = io.BytesIO() fig.savefig(buf, format='png')  # Convert the BytesIO object to a PIL Image buf.seek(0) img = Image.open(buf)  # Save the image img.save('pil_image_save.png') 

Output

saving-plot
saving plot

Next Article
Save Plot To Numpy Array using Matplotlib

H

hemavatisabu
Improve
Article Tags :
  • Python
  • Python-matplotlib
Practice Tags :
  • python

Similar Reads

  • How to plot data from a text file using Matplotlib?
    Perquisites: Matplotlib, NumPy In this article, we will see how to load data files for Matplotlib. Matplotlib is a 2D Python library used for Date Visualization. We can plot different types of graphs using the same data like: Bar GraphLine GraphScatter GraphHistogram Graph and many. In this article,
    3 min read
  • How to Create Matplotlib Plots Without a GUI
    To create and save plots using Matplotlib without opening a GUI window, you need to configure Matplotlib to use a non-interactive backend. This can be achieved by setting the backend to 'Agg', which is suitable for generating plots without displaying them. Let's see how to set the backend to Agg: Me
    2 min read
  • Save Plot To Numpy Array using Matplotlib
    Saving a plot to a NumPy array in Python is a technique that bridges data visualization with array manipulation allowing for the direct storage of graphical plots as array representations, facilitating further computational analyses or modifications within a Python environment. Let's learn how to Sa
    4 min read
  • How to plot a complex number in Python using Matplotlib ?
    In this article we will learn how to plot complex number in Python using Matplotlib. Let's discuss some concepts : Matplotlib : Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designe
    3 min read
  • How to add a legend to a scatter plot in Matplotlib ?
    In this article, we are going to add a legend to the depicted images using matplotlib module. We will use the matplotlib.pyplot.legend() method to describe and label the elements of the graph and distinguishing different plots from the same graph.  Syntax: matplotlib.pyplot.legend( ["title_1", "Titl
    2 min read
  • How to Plot Mfcc in Python Using Matplotlib?
    Mel-frequency cepstral coefficients (MFCC) are widely used in audio signal processing and speech recognition tasks. They represent the spectral characteristics of an audio signal and are commonly used as features for various machine-learning applications. In this article, we will explore how to comp
    2 min read
  • 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
  • How to make a basic Scatterplot using Python-Plotly?
    Plotly is a graphing library for making high quality interactive graphs and charts. It is an open source library used for data visualization . This library can be used with three programming languages namely Python, R and Javascript. Using Plotly is very easy and you can make any type of graph using
    2 min read
  • How to save Matplotlib Animation?
    In this article, we will learn How to save Matplotlib Animation. The animated graphs made with the help of matplotlib can be saved as videos in Python. As we can create captivating animations using the matplotlib library. If you want to learn to create animations, here is a link to the article to cr
    2 min read
  • How to add text to Matplotlib?
    Matplotlib is a plotting library in Python to visualize data, inspired by MATLAB, meaning that the terms used (Axis, Figure, Plots) will be similar to those used in MATLAB. Pyplot is a module within the Matplotlib library which is a shell-like interface to Matplotlib module.  It provides almost any
    5 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