ADVERTISEMENT
ADVERTISEMENT

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 = {
    "Name":['Hari','Mohan','Neeti','Shaily','Ram','Umesh','Shirish','Rashmi','Pradeep','Neelam','Jitendra','Manoj','Rishi'],
    "Age":[25,36,26,21,30,33,35,40,39,45,42,39,48],
    "Gender":['Male','Male','Female','Female','Male','Male','Male','Female','Male','Female','Male','Male','Male'],
    "Profession":['Doctor','Teacher','Singer','Student','Engineer','CA','Cricketer','Teacher','Teacher','Politician',
                 'Doctor','Manager','Clerk'],

    "Title":['Mr','Mr','Ms','Ms','Mr','Mr','Mr','Ms','Mr','Ms','Mr','Mr','Mr'],
    "Salary":[200000,50000,500000,0,100000,75000,10000000,50000,50000,200000,200000,150000,15000],
    "Location":['Amritsar','Indore','Mumbai','Bhopal','Gurugram','Pune','Banglore','Ranchi','Surat','Chennai','Shimla','Kolkata','Raipur'],
    "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
print(result1)
print(result2)

Output:

Salary:

Example 1: Save a Seaborn plot into a file

Age:

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
result1.savefig('save_as_a_png.png')

# Result 2
result2.savefig('save_as_a_png.png')

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

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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

© https://www.includehelp.com some rights reserved.