How to Save Seaborn Plot to a File in Python?
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.
Stepwise Implementation
Step 1:
Import the inbuilt penguins dataset from seaborn package using the inbuilt function load_dataset.
Syntax:
seaborn.load_dataset(name, cache=True, data_home=None, **kws)
Example:
Python3
# code# Install seaborn using pip install seaborn# Import the seaborn packageimport seaborn as sns # load the inbuilt "penguins" dataset using # seaborn inbuilt function load_datasetdata = sns.load_dataset("penguins") # print the first 6 datadata.head() |
Output:
Step 2:
For this example, let us use the scatterplot to check whether is there any relation between bill_length_mm and bill_depth_mm of Male and Female penguins.
Syntax:
seaborn.scatterplot(x, y, hue, style, size, data, palette, hue_order, legend)
Step 3:
This is the prime step to note for saving the plot to the desired file type. Use the Seaborn get_figure function to get the figure plotted in Step 2.
Syntax:
figure_name.get_figure()
The get_figure function stores the output graph to a variable. Store the plot temporarily in a variable as shown below
3a: 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
# Install seaborn using pip install seaborn# Import the seaborn packageimport seaborn as sns # load the inbuilt "penguins" dataset using # seaborn inbuilt function load_datasetdata = sns.load_dataset("penguins") 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:
3b: Saving the Seaborn graph as .jpg
Python3
# code# Install seaborn using pip install seaborn# Import the seaborn packageimport seaborn as sns # load the inbuilt "penguins" dataset using# seaborn inbuilt function load_datasetdata = sns.load_dataset("penguins") 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 # in 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.jpg') # this will store the plot in current working directory |
Output:
3c: Saving the Seaborn graph as .tiff
Python3
# code# Install seaborn using pip install seaborn# Import the seaborn packageimport seaborn as sns # load the inbuilt "penguins" dataset using # seaborn inbuilt function load_datasetdata = sns.load_dataset("penguins") 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 # in 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.tiff') # this will store the plot in current working directory |
Output:
Step 4:
If you wish to save the seaborn plot to a specific folder, then follow the steps mentioned below
Python3
# Install seaborn using pip install seaborn# Import the seaborn packageimport seaborn as sns # load the inbuilt "penguins" dataset using # seaborn inbuilt function load_datasetdata = sns.load_dataset("penguins") 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 # in 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(r'C:\Users\Documents\test\Plots\scatterplot.png') # this will store the plot in specified directory |
Output:




