Seaborn - Sort Bars in Barplot - GeeksforGeeks

09 Dec, 2021
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 attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas.
Import module.
Create a Dataframe.
Create a barplot.
Sort the Dataframe column with DataFrame.sort_values().
Display the sort Dataframe into the barplot.
Step 1: Import required packages.
Python3
# Import moduleimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns
 Step 2: Create a Dataframe to create a barplot. 
Python3
# Initialize dataState = ["Rajasthan", "Bihar", "Madhya Pradesh",         "Gujarat", "Maharashtra"]growth = [342239, 94163, 308245, 196024, 307713] # Create a pandas dataframedf = pd.DataFrame({"State": State,                   "Growth": growth}) # Display Dataframedf
Output:
Step 3: Create a barplot with this Dataframe.
Python3
# make barplotsns.barplot(x='State', y="Growth", data=df)
Output:
Step 4: Let’s sort the Dataframe column(Growth column) with DataFrame.sort_values().
Python3
# sort dataframedf.sort_values('Growth')
Output: 
We can do this same technique in a barplot.
Python3
# make barplot and sort barssns.barplot(x='State',            y="Growth", data=df,            order=df.sort_values('Growth').State)
Output:
Step 5: Let sort the bar with Descending order.
Python3
# make barplot and sort barssns.barplot(x='State',            y="Growth", data=df,            order=df.sort_values('Growth',ascending = False).State)
Output:
Note: Default value of ascending is always True, if we change this parameter with False then it means that its arrange in descending order.
Below are the complete examples:
Example 1
Python3
# import moduleimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns  # Initialize dataState = ["Rajasthan", "Bihar", "Madhya Pradesh",         "Gujarat", "Maharashtra"]growth = [342239, 94163, 308245, 196024, 307713]  # Create a pandas dataframedf = pd.DataFrame({"State": State,                   "Growth": growth})  # sort dataframedf.sort_values('Growth')  # make barplot and sort barssns.barplot(x='State',            y="Growth", data=df,            order=df.sort_values('Growth').State)
Output:
Example 2
Python3
# import moduleimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns  # Initialize dataState = ["Rajasthan", "Bihar", "Madhya Pradesh",         "Gujarat", "Maharashtra"]growth = [342239, 94163, 308245, 196024, 307713]  # Create a pandas dataframedf = pd.DataFrame({"State": State,                   "Growth": growth})  # sort dataframedf.sort_values('Growth')  # make barplot and sort barssns.barplot(x='State',            y="Growth", data=df,            order=df.sort_values('Growth',ascending = False).State)
Output: 
 
surindertarika1234
Python-Seaborn
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Taking input in Python
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python