Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Seaborn | Style And Color
Next article icon

Plotting graph using Seaborn | Python

Last Updated : 08 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

This article will introduce you to graphing in Python with Seaborn, which is the most popular statistical visualization library in Python.

Installation: The easiest way to install seaborn is to use pip. Type following command in terminal:  

pip install seaborn

OR, you can download it from here and install it manually.  

Plotting categorical scatter plots with Seaborn

Stripplot

Python3

# Python program to illustrate
# Plotting categorical scatter 
# plots with Seaborn
  
# importing the required module
import matplotlib.pyplot as plt
import seaborn as sns
  
# x axis values
x =['sun', 'mon', 'fri', 'sat', 'tue', 'wed', 'thu']
  
# y axis values
y =[5, 6.7, 4, 6, 2, 4.9, 1.8]
  
# plotting strip plot with seaborn
ax = sns.stripplot(x, y);
  
# giving labels to x-axis and y-axis
ax.set(xlabel ='Days', ylabel ='Amount_spend')
  
# giving title to the plot
plt.title('My first graph');
  
# function to show plot
plt.show()
                      
                       

Output:

Explanation: This is the one kind of scatter plot of categorical data with the help of seaborn.  

  • Categorical data is represented on the x-axis and values correspond to them represented through the y-axis.
  • .striplot() function is used to define the type of the plot and to plot them on canvas using.
  • .set() function is used to set labels of x-axis and y-axis.
  • .title() function is used to give a title to the graph.
  • To view plot we use .show() function.

Stripplot using inbuilt data-set given in seaborn : 

Python3

# Python program to illustrate
# Stripplot using inbuilt data-set
# given in seaborn
  
# importing the required module
import matplotlib.pyplot as plt
import seaborn as sns
  
# use to set style of background of plot
sns.set(style="whitegrid")
  
# loading data-set
iris = sns.load_dataset('iris')
  
# plotting strip plot with seaborn
# deciding the attributes of dataset on
# which plot should be made
ax = sns.stripplot(x='species', y='sepal_length', data=iris)
  
# giving title to the plot
plt.title('Graph')
  
# function to show plot
plt.show()
                      
                       

Output: 


Explanation:

  • iris is the dataset already present in seaborn module for use.
  • We use .load_dataset() function in order to load the data.We can also load any other file by giving the path and name of the file in the argument.
  • .set(style=”whitegrid”) function here is also use to define the background of plot.We can use “darkgrid” 
    instead of whitegrid if we want the dark-colored background.
  • In .stripplot() function we have to define which attribute of the dataset to be on the x-axis and which attribute of the dataset should on y-axis.data = iris means attributes which we define earlier should be taken from the given data.
  • We can also draw this plot with matplotlib but the problem with matplotlib is its default parameters. The reason why Seaborn is so great with DataFrames is, for example, labels from DataFrames are automatically propagated to plots or other data structures as you see in the above figure column name species comes on the x-axis and column name stepal_length comes on the y-axis, that is not possible with matplotlib. We have to explicitly define the labels of the x-axis and y-axis.

Swarmplot 

Python3

# Python program to illustrate
# plotting using Swarmplot
  
# importing the required module
import matplotlib.pyplot as plt
import seaborn as sns
  
# use to set style of background of plot
sns.set(style="whitegrid")
  
# loading data-set
iris = sns.load_dataset('iris')
  
# plotting strip plot with seaborn
# deciding the attributes of dataset on
# which plot should be made
ax = sns.swarmplot(x='species', y='sepal_length', data=iris)
  
# giving title to the plot
plt.title('Graph')
  
# function to show plot
plt.show()
                      
                       

Output: 

Explanation: 
This is very much similar to stripplot but the only difference is that it does not allow overlapping of markers. It causes jittering in the markers of the plot so that graph can easily be read without information loss as seen in the above plot. 
 

  • We use .swarmplot() function to plot swarm plot.
  • Another difference that we can notice in Seaborn and Matplotlib is that working with DataFrames doesn’t go quite as smoothly with Matplotlib, which can be annoying if we doing exploratory analysis with Pandas. And that’s exactly what Seaborn does easily, the plotting functions operate on DataFrames and arrays that contain a whole dataset.

Note: If we want we can also change the representation of data on a particular axis. 

Example : 

Python3

# importing the required module
import matplotlib.pyplot as plt
import seaborn as sns
  
# use to set style of background of plot
sns.set(style="whitegrid")
  
# loading data-set
iris = sns.load_dataset('iris')
  
# plotting strip plot with seaborn
# deciding the attributes of dataset on 
# which plot should be made
ax = sns.swarmplot(x='sepal_length', y='species', data=iris)
  
  
# giving title to the plot
plt.title('Graph')
  
# function to show plot
plt.show()
                      
                       

Output: 

The same can be done in striplot. At last, we can say that Seaborn is an extended version of matplotlib which tries to make a well-defined set of hard things easy.

Barplot

A barplot is basically used to aggregate the categorical data according to some methods and by default it’s the mean. It can also be understood as a visualization of the group by action. To use this plot we choose a categorical column for the x-axis and a numerical column for the y-axis, and we see that it creates a plot taking a mean per categorical column.

Syntax:

barplot([x, y, hue, data, order, hue_order, …])

Python3

# import the seaborn library 
import seaborn as sns 
  
# reading the dataset 
df = sns.load_dataset('tips') 
  
# change the estimator from mean to
# standard deviation 
sns.barplot(x ='sex', y ='total_bill', data = df,  
            palette ='plasma')
                      
                       

Output:

Explanation:
Looking at the plot we can say that the average total_bill for the male is more than compared to the female.

  • Palette is used to set the color of the plot
  • The estimator is used as a statistical function for estimation within each categorical bin.

Countplot

A countplot basically counts the categories and returns a count of their occurrences. It is one of the simplest plots provided by the seaborn library.

Syntax:

countplot([x, y, hue, data, order, …])

Python3

# import the seaborn library 
import seaborn as sns 
  
# reading the dataset 
df = sns.load_dataset('tips') 
  
sns.countplot(x ='sex', data = df) 
                      
                       

Output:

Explanation:

Looking at the plot we can say that the number of males is more than the number of females in the dataset. As it only returns the count based on a categorical column, we need to specify only the x parameter.

Boxplot

Box Plot is the visual representation of the depicting groups of numerical data through their quartiles. Boxplot is also used to detect the outlier in the data set.

Syntax:

boxplot([x, y, hue, data, order, hue_order, …])

Python3

# import the seaborn library
import seaborn as sns
  
# reading the dataset
df = sns.load_dataset('tips')
  
sns.boxplot(x='day', y='total_bill', data=df, hue='smoker')
                      
                       

Output:

Explanation:

x takes the categorical column and y is a numerical column. Hence we can see the total bill spent each day.” hue” parameter is used to further add a categorical separation. By looking at the plot we can say that the people who do not smoke had a higher bill on Friday as compared to the people who smoked.

Violinplot

It is similar to the boxplot except that it provides a higher, more advanced visualization and uses the kernel density estimation to give a better description about the data distribution.

Syntax:

violinplot([x, y, hue, data, order, …])

Python3

# import the seaborn library
import seaborn as sns
  
# reading the dataset
df = sns.load_dataset('tips')
sns.violinplot(x='day', y='total_bill', data=df,
               hue='sex', split=True)
                      
                       

Output:

Explanation:

  • hue is used to separate the data further using the sex category
  • setting split=True will draw half of a violin for each level. This can make it easier to directly compare the distributions.

Stripplot

It basically creates a scatter plot based on the category.

Syntax:

stripplot([x, y, hue, data, order, …])

Python3

# import the seaborn library
import seaborn as sns
  
# reading the dataset
df = sns.load_dataset('tips')
sns.stripplot(x='day', y='total_bill', data=df,
              jitter=True, hue='smoker', dodge=True)
                      
                       

Output:

Explanation:

  • One problem with strip plot is that you can’t really tell which points are stacked on top of each other and hence we use the jitter parameter to add some random noise.
  • jitter parameter is used to add an amount of jitter (only along the categorical axis) which can be useful when you have many points and they overlap so that it is easier to see the distribution.
  • hue is used to provide an additional categorical separation
  • setting split=True is used to draw separate strip plots based on the category specified by the hue parameter.


Next Article
Seaborn | Style And Color

S

saloni1297
Improve
Article Tags :
  • Machine Learning
  • python
Practice Tags :
  • Machine Learning
  • python

Similar Reads

    Introduction

    • Seaborn is a library mostly used for statistical plotting in Python. It is built on top of Matplotlib and provides beautiful default styles and color palettes to make statistical plots more attractive. In this tutorial, we will learn about Python Seaborn from basics to advance using a huge dataset o
      15+ min read

    • Prerequisite - Matplotlib Library  Visualization is an important part of storytelling, we can gain a lot of information from data by simply just plotting the features of data. Python provides a numerous number of libraries for data visualization, we have already seen the Matplotlib library in this a
      5 min read

    • This article will introduce you to graphing in Python with Seaborn, which is the most popular statistical visualization library in Python. Installation: The easiest way to install seaborn is to use pip. Type following command in terminal: pip install seaborn OR, you can download it from here and ins
      8 min read

    Styling Plots

    • Seaborn is a statistical plotting library in python. It has beautiful default styles. This article deals with the ways of styling the different kinds of plots in seaborn. Seaborn Figure Styles This affects things like the color of the axes, whether a grid is enabled by default, and other aesthetic e
      4 min read

    • In this article, We are going to see seaborn color_palette(), which can be used for coloring the plot. Using the palette we can generate the point with different colors. Example: [GFGTABS] Python import seaborn as sns import matplotlib.pyplot as plt # Set a Seaborn color palette sns.set_palette(
      3 min read

    Multiple Plots

    • Prerequisite: Seaborn Programming Basics Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Seaborn helps resolve the two major problems faced by Matplotlib; the problems are ? Default Ma
      3 min read

    • Prerequisite: Seaborn Programming Basics Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Seaborn helps resolve the two major problems faced by Matplotlib; the problems are ? Default Ma
      3 min read

    Scatter Plot

    • Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas.
      4 min read

    • To understand how variables in a dataset are related to one another and how that relationship is dependent on other variables, we perform statistical analysis. This Statistical analysis helps to visualize the trends and identify various patterns in the dataset. One of the functions which can be used
      2 min read

    • In this article, we will learn how to male scatter plots with regression lines using Seaborn in Python. Let's discuss some concepts : Seaborn : Seaborn is a tremendous visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make s
      2 min read

    • Prerequisites: Seaborn  Scatter Plot with Marginal Histograms is basically a joint distribution plot with the marginal distributions of the two variables. In data visualization, we often plot the joint behavior of two random variables (bi-variate distribution) or any number of random variables. But
      2 min read

    Line Plot

    • Prerequisite: SeabornMatplotlib  Presenting data graphically to emit some information is known as data visualization. It basically is an image to help a person interpret what the data represents and study it and its nature in detail. Dealing with large scale data row-wise is an extremely tedious tas
      4 min read

    • In this article, we will learn how to create A Time Series Plot With Seaborn And Pandas. Let's discuss some concepts : Pandas is an open-source library that's built on top of NumPy library. It's a Python package that gives various data structures and operations for manipulating numerical data and st
      4 min read

    • Time Series Plot is used to observe various trends in the dataset over a period of time. In such problems, the data is ordered by time and can fluctuate by the unit of time considered in the dataset (day, month, seconds, hours, etc.). When plotting the time series data, these fluctuations may preven
      4 min read

    Bar Plot

    • Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas.  Se
      7 min read

    • Prerequisite: Seaborn, Barplot In this article, we are going to see how to sort the bar in barplot using Seaborn in python. Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more
      3 min read

    Count Plot

    • seaborn.countplot() is a function in the Seaborn library in Python used to display the counts of observations in categorical data. It shows the distribution of a single categorical variable or the relationship between two categorical variables by creating a bar plot. Example: [GFGTABS] Python import
      8 min read

    Box Plot

    • Boxplot is used to see the distribution of numerical data and identify key stats like minimum and maximum values, median, identifying outliers, understanding how data is distributed and can compare the distribution of data across different categories or variables. In Seaborn the seaborn.boxplot() fu
      3 min read

    • Prerequisite: seaborn The Boxplots are used to visualize the distribution of data which is useful when a comparison of data is required. Sometimes, Boxplot is also known as a box-and-whisker plot. The box shows the quartiles of dataset and whiskers extend to show rest of the distribution. In this ar
      1 min read

    • Adding the right set of color with your data visualization makes it more impressive and readable, seaborn color palettes make it easy to use colors with your visualization. In this article, we will see how to color boxplot with seaborn color palettes also learn the uses of seaborn color palettes and
      2 min read

    • A boxplot is a powerful data visualization tool used to understand the distribution of data. It splits the data into quartiles, and summarises it based on five numbers derived from these quartiles: median: the middle value of data. marked as Q2, portrays the 50th percentile.first quartile: the middl
      2 min read

    • Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas.Se
      3 min read

    • Boxplot depicts the distribution of quantitative data facilitating comparisons between different variables, continuous or categorical. It is a common data dispersion measure. Boxplots consist of a five-number summary which helps in detecting and removing outliers from the dataset. Minimum observatio
      2 min read

    • Box Plot is the visual representation of the depicting groups of numerical data through their quartiles. Boxplot is also used for detect the outlier in data set. It captures the summary of the data efficiently with a simple box and whiskers and allows us to compare easily across groups. Boxplot summ
      2 min read

    Violin Plot

    • Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas. V
      7 min read

    • In this article, we are going to plot a horizontal Violin plot with seaborn. We can use two methods for the Drawing horizontal Violin plot, Violinplot() and catplot(). Method 1: Using violinplot() A violin plot plays a similar activity that is pursued through whisker or box plot do. As it shows seve
      3 min read

    • This article depicts how to make a grouped violinplot with Seaborn in python. Violinplot is a great way of visualizing the data as a combination of the box plot with the kernel density plots to produce a new type of plot.  For this article, we will be using the iris dataset to plot data. This comes
      3 min read

    Strip Plot

    • Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on top of the matplotlib library and also closely integrated into the data structures from pandas. S
      5 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences