Saving a Plot as an Image in Python

In this article, we would learn how to save a plot as an image in python. There are times when one needs a matplotlib figure as an image file so that we can use it for other purposes.

We can save a plot as an image easily by following the steps mentioned in this article.  So let us begin.

How to save a matplotlib plot as an image in Python

In the previous article: Line Chart Plotting in Python using Matplotlib we have seen the following plot.

save a plot as an image in matplotlib python

Now we’ll see how to save this plot.

We can save a matplotlib plot by using the savefig( ) function. This function saves the figure in the current working directory. We can give a name, formats such as .jpg, .png etc and a resolution in dpi (dots per inches) to the saved image.

Consider the graph as shown above and its code

import matplotlib.pyplot as plt 
 
# Declaring the points for first line plot
X1 = [1,2,3,4,5] 
Y1 = [2,4,6,8,10] 
# plotting the first plot
plt.plot(X1, Y1, label = "plot 1") 

# Declaring the points for second line plot
X2 = [1,2,3,4,5] 
Y2 = [1,4,9,16,25]

# plotting the second plot 
plt.plot(X2, Y2, label = "plot 2") 
  
# Labeling the X-axis 
plt.xlabel('X-axis') 

# Labeling the Y-axis 
plt.ylabel('Y-axis') 

# Give a title to the graph
plt.title('Two plots on the same graph') 
  
# Show a legend on the plot 
plt.legend() 
 
plt.show()

We can save this graph by adding two lines in the above code

  1. fig = plt.figure( ) , added before the plot function.
  2. fig.saveplot( ) , added after plot and before plt.show( ) .

 

Code to save the plot as an image – matplotlib

import matplotlib.pyplot as plt 
 
# Declaring the points for first line plot
X1 = [1,2,3,4,5] 
Y1 = [2,4,6,8,10] 

# Setting the figure size
fig = plt.figure(figsize=(10,5))

# plotting the first plot
plt.plot(X1, Y1, label = "plot 1") 

# Declaring the points for second line plot
X2 = [1,2,3,4,5] 
Y2 = [1,4,9,16,25]

# plotting the second plot 
plt.plot(X2, Y2, label = "plot 2") 
  
# Labeling the X-axis 
plt.xlabel('X-axis') 

# Labeling the Y-axis 
plt.ylabel('Y-axis') 

# Give a title to the graph
plt.title('Two plots on the same graph') 
  
# Show a legend on the plot 
plt.legend() 

#Saving the plot as an image
fig.savefig('line plot.jpg', bbox_inches='tight', dpi=150)

#Showing the plot
plt.show() 

As seen in the above code, we added two extra lines to save our plot to the current working directory. You can find the current working directory using the os module of python.

Run the following code to find the current directory

import os
os.getcwd()

Note:

  • We can set the figure size with the plt.figure( ) function.
  • plt.figure() function is called first and then the plotting function.
  • fig.savefig() function is called before the plt.show( ).

Savefig( ) function explanation

plt.savefig('line plot.jpg',bbox_inches='tight', dpi=150)

Here, ‘line plot.jpg’ is the name of the figure saved with the extension as ‘.jpg’. We can give any extension like ‘.png ‘ , ‘.jpeg’ etc. We can also decide the resolution of the saved image by changing the dpi argument. A dpi of 75 is good if you want to put the image on a web page or a dpi of 250 or more is good if the image is to be included in a report or docx file. The argument bbox_inches=’tight’ is optional and is set if the labels of the axes are cut off in the saved image.

There are also other parameters in the savefig( ) command. You can refer to the documentation of this command by following the link: matplotlib.pyplot.savefig

I hope you liked the article. Comment if you have any doubts or suggestions regarding this article.

Leave a Reply

Your email address will not be published. Required fields are marked *