How to Change the Order of Bars in Seaborn Barplot


You can use the following methods to change the order of bars in a seaborn 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.

Additional Resources

The following tutorials explain how to perform other common functions in seaborn:

How to Show Values on Seaborn Barplot
How to Create a Grouped Barplot in Seaborn
How to Set the Color of Bars in a Seaborn Barplot

Leave a Reply

Your email address will not be published. Required fields are marked *