Matplotlib – Save Plot as a File

Matplotlib is a library in python that offers a number of plotting options to display your data. The plots created get displayed when you use plt.show() but you cannot access them later since they’re not saved on disk. In this tutorial, we’ll look at how to save a matplotlib plot as an image file.

To save a figure created with matplotlib, you can use pyplot’s savefig() function. This way, you’ll have the plots saved on disk for further use instead of having to plot them all over again. The following is the syntax:

import matplotlib.pyplot as plt
plt.savefig("filename.png")

Pass the path where you want the image to be saved. The savefig() function also comes with a number of additional parameters to further customize how your image gets saved.

Let’s look at some examples of using plt.savefig() to save a maplotlib figure.

Let’s say you have the data of some of NBA’s greatest basketball players’ championship title counts and you want to plot it as a bar chart. Furthermore, you want to save it as an image so that you can use it later (for example, in a presentation)

import matplotlib.pyplot as plt

# NBA championship counts
players = ['Kobe Bryant', 'LeBron James', 'Michael Jordan', 'Larry Bird']
titles = [5,4,6,3]

# plot a bar chart
plt.bar(players, titles)
# add y-axis label
plt.ylabel("Rings")
# add chart title
plt.title("Championship Victories of NBA greats")

# save the plot as a PNG image
plt.savefig("NBA_bar_chart.png")

This saves the bar chart as a PNG file with the name NBA_bar_chart.png to the current directory. You can specify the path and name of your image as per your needs. This is how the saved plot looks on opening it with an image viewer application:

Bar chart saved on disk opened with the Photos application in Windows

Depending on the filename provided plt.savefig() infers the format of the output file. For instance, if you want to save the above image as a PDF file, just use the appropriate file name:

import matplotlib.pyplot as plt

# NBA championship counts
players = ['Kobe Bryant', 'LeBron James', 'Michael Jordan', 'Larry Bird']
titles = [5,4,6,3]

# plot a bar chart
plt.bar(players, titles)
# add y-axis label
plt.ylabel("Rings")
# add chart title
plt.title("Championship Victories of NBA greats")

# save the plot as a PDF
plt.savefig("NBA_bar_chart_doc.pdf")

The above code saves the plot as a PDF file with the name NBA_bar_chart_doc.pdf to the current directory. This is how the saved images looks like on opening it in Google Chrome web browser:

Matplotlib plot saved as a pdf file opened in chrome

For more on the plt.savefig() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having matplotlib version 3.2.2


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.