How to save a Seaborn plot into a file?

Given a DataFrame, we have to save a Seaborn plot into a file.
Submitted by Pranit Sharma, on May 29, 2022

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.

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.)

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example.

# 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



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.

# 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!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.