How to save a Seaborn plot into a file?

Given a DataFrame, we have to save a Seaborn plot into a file. By Pranit Sharma Last updated : September 21, 2023

For statistical graphics or pictorial representation of statistical data, python provides us with a library called seaborn.

This library is closely related to pandas data structure and also it is based upon the concepts of the matplotlib library.

While dealing with a huge amount of data, we may need a good graphical interface to visualize and analyze the data effectively, in that case, we need some python libraries and here the role of seaborn comes into the role.

The seaborn represents our data which is stored in an array, list, or even a complete DataFrame in graphical format for easy understanding.

Here, we are going to understand, how to save a seaborn plot into a file.

Saving a Seaborn plot into a file

To save a seaborn plot into a file, we need to follow 4 easy steps,

  • Import
  • Load
  • Create
  • Save

First, we need to import the required libraries, then we need to load our data (or we can create a DataFrame manually too). The third step is to create a plot and finally, we will save our plot into a file of the required format (pdf, png, etc.)

Let us understand with the help of an example.

Python code to create dataframe and Seaborn plot and display

# Importing pandas package
import pandas as pd

# Importing matplotlib package
import matplotlib

# Importing seaborn package
import seaborn as sns

# Creating a dictionary
d = {

    "Marriage Status":[0,1,1,0,1,0,0,1,1,1,0,1,0]

# Creating a DataFrame
df = pd.DataFrame(d)

# Creating a plot
result1 = sns.displot(df['Salary'])
result2 = sns.displot(df['Age'])

# Display result


The output of the above program is:


Example 1: Save a Seaborn plot into a file


Example 2: Save a Seaborn plot into a file

Until now, we have imported the required libraries, loaded the dataset and created the plot. Now the final step is to save this plot into a file.

For this purpose, we will use plt.savefig() method which will allow us to save the plots into our required format.

Python code to save a Seaborn plot into a file

# Saving the file

# Result 1

# Result 2

There are certain other parameters for the format of the file in which we want to save our file.

Python Pandas Programs »

Comments and Discussions!

Copyright © 2023 All rights reserved.