Saving a Plot as an Image in Python
Last Updated :
30 Dec, 2022
Sometimes we want to save charts and graphs as images or even images as a file on disk for use in presentations to present reports. And in some cases, we need to prevent the plot and images for future use. In this article, we’ll see a way to save a plot as an image by converting it to an image, and we’ll also see how to use the save file feature in some of the most popular data visualizations and plotting Python libraries to save plots as an image.
Saving a plot as an image by converting it to a PIL Image in Python
Below are some approaches which we followed to Save a Plot as an Image.
- Importing necessary library for plotting.
- Creating a list or loading data for plotting a line graph
- Plotting line Graph with the created list or load data.
- Create a Function for Converting a passed figure to a PIL Image.
- Save the return image in a variable by passing a plot in the created function for Converting a plot to a PIL Image.
- Save the image with the help of the save() Function.
Python3
import numpy as np
import matplotlib.pyplot as plt
import io
from PIL import Image
Month = [ 'January' , 'February' , 'March' ]
Share_buy = [ 10 , 17 , 30 ]
plt.title( "Share's Buy in a month" )
plt.xlabel( 'Months' )
plt.ylabel( "No of Share's" )
plt.plot(Month, Share_buy)
fig = plt.gcf()
def fig2img(fig):
buf = io.BytesIO()
fig.savefig(buf)
buf.seek( 0 )
img = Image. open (buf)
return img
img = fig2img(fig)
img.save( 'Plot image.png' )
|
Output File:
Saved Plot as an Image in Python
Saving a Matplotlib Plot as an Image in Python
Below are some approaches which we followed to Save a Plot as an Image.
- Importing necessary libraries for plotting like Matplotlib.
- Creating a list or loading data for plotting a line graph
- Plotting line Graph with the created list or load data.
- Saving a plot on disk as an image file with the help of Matplotlib ‘savefig( )‘ function.
Python3
import matplotlib.pyplot as plt
Month = [ 'January' , 'February' , 'March' ]
Share_buy = [ 10 , 17 , 30 ]
plt.title( "Share's Buy in a month" )
plt.xlabel( 'Months' )
plt.ylabel( "No of Share's" )
plt.plot(Month, Share_buy)
plt.savefig( 'plot image.png' )
plt.show()
|
Output:
Line Chart using Matplotlib
Output File:
Saved Plot as an Image in Python
Saving a Seaborn Plot as an Image in Python
Below are some approaches which we followed to Save a Plot as an Image.
- Importing necessary library for plotting like seaborn.
- Creating a list or load data for plotting a Scatter Plot
- Plotting Scatter plot with the loaded data.
- Saving a plot on disk as an image file with the help of ‘savefig( )‘ function.
Python3
import seaborn as sns
data = sns.load_dataset( 'iris' )
scatter_plot = sns.scatterplot(x = data[ 'sepal_length' ],
y = data[ 'petal_length' ],
hue = data[ 'species' ])
scatter_fig = scatter_plot.get_figure()
scatter_fig.savefig( 'Seaborn scatterplot.png' )
|
Output:
Scatter Plot using Seaborn
Output File:
Saved Plot as an Image in Python
Saving a Plotly Plot as an Image in Python
Note: In plotly Static image generation requires Kaleido library. So to save plotly plot as an image, install Kaleido library in your system.
Below are some approaches which we followed to Save a Plot as an Image.
- Importing necessary library for plotting like plotly.
- Creating a list or loading data for plotting a line graph
- Plotting line Graph with the created list or load data.
- Saving a plotly plot on disk as an image file with the help of the ‘write_image()‘ function.
Python3
import plotly.express as px
Month = [ 'January' , 'February' , 'March' ]
Share_buy = [ 10 , 17 , 30 ]
fig = px.line(x = Month, y = Share_buy, title = "Share's Buy in a month" )
fig.write_image( "plotly plot.png" )
fig.show()
|
Output:
Output File:
Saved Plot as an Image in Python
Saving an Interactive Plot as an Image in Python
There are times when we create 3D visualizations then it requires interactive features to carefully analyze the graph. To enable these features in the Matplotlib library then we will have to add inline magic in the python code that is %matplotlib notebook after adding this the graph which is created have different options to save, zoom and focus on a selected rectangular area.
Python3
% matplotlib notebook
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
xs = [ 2 , 3 , 4 , 5 , 1 , 6 , 2 , 1 , 7 , 2 ]
ys = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
zs = np.zeros( 10 )
dx = np.ones( 10 )
dy = np.ones( 10 )
dz = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
figg = plt.figure()
ax = figg.add_subplot( 111 , projection = '3d' )
plot_geeks = ax.bar3d(xs, ys, zs, dx,
dy, dz, color = 'green' )
ax.set_title( "3D bar plot" )
ax.set_xlabel( 'x-axis' )
ax.set_ylabel( 'y-axis' )
ax.set_zlabel( 'z-axis' )
plt.savefig( 'my_plot1.png' )
plt.close()
|
Output:
In the above interactive plot, there is an option there to download the image, and hence using this we can download or save an interactive plot in Python. Instead of using plt.show() if we will use plt.close() then this interactive feature of the plot will disappear automatically.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...