Table of Contents
Seaborn is a powerful Python library used for data visualization. One of its features is the ability to create visually appealing barplots. However, the default order of bars in a barplot may not always be ideal for the data being displayed. In order to change the order of bars in a Seaborn barplot, the user can use the “order” parameter and specify the desired order of the bars. This can be done by passing a list of the desired order to the “order” parameter. Alternatively, the user can also use the “hue” parameter to group the data and change the order of the bars within each group. By using these parameters, the user can easily customize the order of bars in a Seaborn barplot to effectively communicate their data.
Change the Order of Bars in Seaborn Barplot
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 28We 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.
The following tutorials explain how to perform other common functions in seaborn:
Cite this article
stats writer (2024). How can I change the order of bars in a Seaborn barplot?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-change-the-order-of-bars-in-a-seaborn-barplot/
stats writer. "How can I change the order of bars in a Seaborn barplot?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-change-the-order-of-bars-in-a-seaborn-barplot/.
stats writer. "How can I change the order of bars in a Seaborn barplot?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-change-the-order-of-bars-in-a-seaborn-barplot/.
stats writer (2024) 'How can I change the order of bars in a Seaborn barplot?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-change-the-order-of-bars-in-a-seaborn-barplot/.
[1] stats writer, "How can I change the order of bars in a Seaborn barplot?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I change the order of bars in a Seaborn barplot?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
