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 the seaborn package import seaborn as sns # load the inbuilt "penguins" dataset using # seaborn inbuilt function load_dataset data = sns.load_dataset( "penguins" ) # print the first 6 data 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' ]) # use get_figure function and store the plot i # n a variable (scatter_fig) scatter_fig = scatter_plot.get_figure() # use savefig function to save the plot and give # a desired name to the plot. scatter_fig.savefig( 'scatterplot.png' ) # this will store the plot in current working directory |
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' ) # this will store the plot in current working directory |
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' ) # this will store the plot in current working directory |
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' ) # this will store the plot in specified directory |
Output:
Save Seaborn Plot to a File in Python