How to Save Seaborn Plot to a File in Python?
Last Updated :
08 Sep, 2022
Seaborn provides a way to store the final output in different desired file formats like .png, .pdf, .tiff, .eps, etc. Let us see how to save the output graph to a specific file format.
Saving a Seaborn Plot to a File in Python
Import the inbuilt penguins dataset from seaborn package using the inbuilt function load_dataset.
Python3
import seaborn as sns
data = sns.load_dataset( "penguins" )
data.head()
|
Save Seaborn Plot to a File
Saving the plot as .png:
Finally, use savefig function and give the desired name and file type to store the plot. The below example stores the plot as a .png file in the current working directory.
Python3
scatter_plot = sns.scatterplot(
x = data[ 'bill_length_mm' ], y = data[ 'bill_depth_mm' ], hue = data[ 'sex' ])
scatter_fig = scatter_plot.get_figure()
scatter_fig.savefig( 'scatterplot.png' )
|
Output:
Save Seaborn Plot to a File in Python
Saving the Seaborn graph as .jpg
Here we want to save the seaborn graph as a Joint Photographic Experts Group file so we are giving it’s extension as .jpg.
Python3
scatter_fig.savefig( 'scatterplot.jpg' )
|
Output:
Save Seaborn Plot to a File in Python
Saving the Seaborn graph as .tiff
Here we want to save the seaborn graph as Tag Image file format file so we are giving it’s extension as .tiff.
Python3
scatter_fig.savefig( 'scatterplot.tiff' )
|
Output:
Save Seaborn Plot to a File in Python
To save the seaborn plot to a specific folder,
Here we want to save the seaborn graph in a particular folder so we are giving the specified path for saving it.
Python3
scatter_fig.savefig(r 'C:\Users\Documents\test\Plots\scatterplot.png' )
|
Output:
Save Seaborn Plot to a File in Python