Table of Contents
In Seaborn barplots, the order of the bars can be changed by reordering the dataframe categories or by setting the order parameter. The order parameter takes a list of values that correspond to the categories in the dataframe, and the bars will be drawn in the same order as the list. This allows us to customize the order of the bars in the graph.
You can use the following methods to change the order of bars in a plot:
Method 1: Sort Bars in Barplot Created from Raw Data
sns.barplot(x='xvar', y='yvar', data=df, order=df.sort_values('yvar').xvar)
Method 2: Sort Bars in Barplot Created from Aggregated Data
sns.barplot(x='xvar', y='yvar', data=df, order=df_agg['xvar']
The following examples show how to use each method in practice.
Example 1: Sort Bars in Barplot Created from Raw Data
Suppose we have the following pandas DataFrame that contains information about the total sales made by various employees at a company:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'employee': ['Andy', 'Bert', 'Chad', 'Doug', 'Eric', 'Frank'],
'sales': [22, 14, 9, 7, 29, 20]})
#view DataFrame
print(df)
employee sales
0 Andy 22
1 Bert 14
2 Chad 9
3 Doug 7
4 Eric 29
5 Frank 20
We can use the following syntax to create a barplot where the bars are sorted in ascending order based on the sales value:
import seaborn as sns #create barplot with bars sorted by sales values ascending sns.barplot(x='employee', y='sales', data=df, order=df.sort_values('sales').employee)
To instead sort the bars in descending order, simply use ascending=False within the sort_values() function:
import seaborn as sns #create barplot with bars sorted by sales values descending sns.barplot(x='employee', y='sales', data=df, order=df.sort_values('sales', ascending=False).employee)
Example 2: Sort Bars in Barplot Created from Aggregated Data
Suppose we have the following pandas DataFrame that contains information about the total sales made by various employees at a company:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'employee': ['A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
'sales': [24, 20, 25, 14, 19, 13, 30, 35, 28]})
#view DataFrame
print(df)
employee sales
0 A 24
1 A 20
2 A 25
3 B 14
4 B 19
5 B 13
6 C 30
7 C 35
8 C 28
We can use the following syntax to calculate the mean sales value, grouped by employee:
#calculate mean sales by employee df_agg = df.groupby(['employee'])['sales'].mean().reset_index().sort_values('sales') #view aggregated data print(df_agg) employee sales 1 B 15.333333 0 A 23.000000 2 C 31.000000
We can then use the following syntax to create a barplot in seaborn that displays the mean sales by employee with the bars displayed in ascending order:
import seaborn as sns #create barplot with bars ordered in ascending order by mean sales sns.barplot(x='employee', y='sales', data=df, order=df_agg['employee'], errorbar=('ci', False))
The x-axis displays the employee name and the y-axis displays the mean sales value for each employee.