Open In App

Seaborn – Sort Bars in Barplot

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.

Approach:

So, Let’s implement to sort bar in barplot using seaborn with steps based on the above approach.

Step 1: Import required packages.






# Import module
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

 
Step 2: Create a Dataframe to create a barplot




# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
 
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
 
# Display Dataframe
df

Output:

Step 3: Create a barplot with this Dataframe.




# make barplot
sns.barplot(x='State', y="Growth", data=df)

Output:

Step 4: Let’s sort the Dataframe column(Growth column) with DataFrame.sort_values().




# sort dataframe
df.sort_values('Growth')

Output: 

We can do this same technique in a barplot.




# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df,
            order=df.sort_values('Growth').State)

Output:

Step 5: Let sort the bar with Descending order.




# make barplot and sort bars
sns.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




# import module
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
 
 
# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
 
 
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
 
 
# sort dataframe
df.sort_values('Growth')
 
 
# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df,
            order=df.sort_values('Growth').State)

Output:

Example 2




# import module
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
 
 
# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
 
 
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
 
 
# sort dataframe
df.sort_values('Growth')
 
 
# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df,
            order=df.sort_values('Growth',ascending = False).State)

Output: 

 


Article Tags :