Movatterモバイル変換


[0]ホーム

URL:


Open In App

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 inMatplotlib:

  1. Usingsavefig()
  2. Usingmatplotlib.pyplot.imsave()
  3. Using the Pillow Library

1. Usingsavefig() 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
importmatplotlib.pyplotaspltyear=['2010','2002','2004','2006','2008']production=[25,15,35,30,10]# Plotting bar chartplt.bar(year,production)# Saving the plot as a JPEG fileplt.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. Usingmatplotlib.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
importmatplotlib.pyplotaspltimportnumpyasnp# Generate random image dataimg=np.random.rand(100,100)# Save imageplt.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 aPillow (PIL) Image object and save it. This is specifically useful when one is dealing with image processing operations that need Pillow.

  • BytesIO: We use aBytesIO 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
importmatplotlib.pyplotaspltfromPILimportImageimportiofig,ax=plt.subplots()ax.plot([1,2,3],[1,2,3])# Save the plot to a BytesIO objectbuf=io.BytesIO()fig.savefig(buf,format='png')# Convert the BytesIO object to a PIL Imagebuf.seek(0)img=Image.open(buf)# Save the imageimg.save('pil_image_save.png')

Output

saving-plot
saving plot

Improve
Improve

Explore

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

[8]ページ先頭

©2009-2025 Movatter.jp