Save Plot to Image File in Python Matplotlib & seaborn (2 Examples)
Hi! This tutorial will demonstrate how to export a plot as an image file in Matplotlib and seaborn in Python.
Here is an overview:
Let’s get into the discussion!
Install & Import Matplotlib & seaborn
To install and import Matplotlib and seaborn, run the lines of code below in your Python programming IDE or code editor:
# install Matplotlib & seaborn pip install matplotlib seaborn # import Matplotlib & seaborn import matplotlib.pyplot as plt import seaborn as sns
With Matplotlib and seaborn install and imported into our Python programming environment, we can now create the example dataset that we will use in this tutorial.
Create Example Dataset
We will use the diamonds dataset that is shipped with the seaborn library as our example dataset in this tutorial.
Therefore, to load and preview the first 5 rows of the DataFrame, run the lines of code below:
df = sns.load_dataset("diamonds") df.head(5) # carat cut color clarity depth table price x y z #0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43 #1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31 #2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31 #3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63 #4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75
Now that we have loaded the example dataset, we can now build visualizations in both Matplotlib and seaborn, and demonstrate how to save the plots as image files.
Example 1: Export Plot As Image File in Matplotlib
In this example, we will build a simple bar plot and then demonstrate how to save it as an image file:
plt.bar(df["cut"],df["depth"]) plt.xlabel("Diamond cut") plt.ylabel("Depth") plt.show()
In the above example, we built the bar plot using plt.bar() wherein we passed the cut
column of df
to the x-axis and the depth
column to the y-axis.
Then we used plt.xlabel() and plt.ylabel() to set the labels of the x and y axes of the plot, and finally displayed the plot using plt.show().
Now, to save the plot as an image file, we will use the plt.savefig() method like so:
plt.bar(df["cut"],df["depth"]) plt.xlabel("Diamond cut") plt.ylabel("Depth") plt.savefig("barplot.jpg") plt.show()
In order to save the plot as a JPEG image file, all we needed to do was to give the plot a name along with the .jpg
extension in plt.savefig()
. To save the plot as a PNG image file, simply replace the .jpg
extension with .png
.
Note, however, that plt.savefig()
has to be written before plt.show()
, and after the plot has been constructed; otherwise, you will save a blank image file to your directory.
Example 2: Export Plot As Image File in seaborn
In this example, we will build a simple scatter plot and demonstrate how to save the plot as an image file. To build the plot, run the lines of code below:
sns.scatterplot(df, x = "carat", y = "price", hue = "cut") plt.show()
Here, we used the sns.scatterplot() method to build the scatter plot. In it, we passed the DataFrame df
and the carat
column of df
on the x-axis and the price
column on the y-axis. We then displayed the plot using plt.show()
.
Next, we will show how to save the plot as an image file. To do so, run the lines of code below:
sns.scatterplot(df, x = "carat", y = "price", hue = "cut") plt.savefig("scatterplot.jpg") plt.show()
As you can see, we are using plt.savefig()
to save the plot as a JPEG file, just as we did in the Matplotlib example above. As in the Matplotlib example, replacing the .jpg
extension with .png
will save or export the file as a PNG image file. You can also export the plot as a PDF file as well by using the .pdf
extension.
Also, make sure to write plt.savefig()
after sns.scatterplot()
but before plt.show()
. Doing otherwise will result in a blank image being exported.
Video, Further Resources & Summary
Do you need more explanations on how to save a plot to an image file in Python Matplotlib and seaborn? Then, you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to save a plot to an image file in Python Matplotlib and seaborn.
The YouTube video will be added soon.
So, we have demonstrated how to save a plot as an image file in Matplotlib and seaborn. Furthermore, you could have a look at some of the other interesting Matplotlib and seaborn tutorials on Statistics Globe:
- Add Legend to Plot in Python Matplotlib & seaborn (4 Examples)
- Change Legend Size of Plot in Python Matplotlib & seaborn (2 Examples)
- Change Font Size of Plot in Python Matplotlib & seaborn (2 Examples)
- Remove Legend from Plot in Python Matplotlib & seaborn (2 Examples)
- Change Axis Labels of Plot in Python Matplotlib & seaborn (2 Examples)
- Introduction to Python Programming
This post has shown how to save a plot to an image file in Python Matplotlib and seaborn. I hope you found it helpful! In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.