How do I order boxplots on the x-axis in Seaborn?

In order to order the boxplots on the x-axis in Seaborn, you can use the “order” parameter to specify the order you would like the boxplots to appear. For example, you could specify that the boxplots should be ordered from highest to lowest by using the following code: sns.boxplot(x=”myvariable”, data=mydata, order=mydata.myvariable.value_counts().index). This will order the boxplots from highest to lowest on the x-axis.


You can use the following methods to change the order of boxplots along the x-axis in seaborn:

Method 1: Order Boxplots Using Custom Order

sns.boxplot(x='group_var', y='values_var', data=df, order=['A', 'B', 'C'])

Method 2: Order Boxplots Using a Metric

group_means=df.groupby(['group_var'])['values_var'].mean().sort_values(ascending=True)

sns.boxplot(x='group_var', y='values_var', data=df, order=group_means.index)

The following examples show how to use each method in practice with the following pandas DataFrame that shows the points scored by basketball players on three different teams:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B',
                            'B', 'B', 'C', 'C', 'C', 'C', 'C'],
                   'points': [3, 4, 6, 8, 9, 10, 13, 16, 18, 20, 8, 9, 12, 13, 15]})

#view head of DataFrame
print(df.head())

  team  points
0    A       3
1    A       4
2    A       6
3    A       8
4    A       9

Example 1: Order Boxplots Using Custom Order

The following code shows how to create a boxplot to visualize the distribution of points for each team and order the boxplots in the following order based on team name: C, A, B.

import seaborn as sns

#create boxplots with custom order
sns.boxplot(x='team', y='points', data=df, order=['C', 'A', 'B'])

Notice that the boxplots are ordered along the x-axis in the exact order that we specified.

Example 2: Order Boxplots Using a Metric

The following code shows how to create a boxplot to visualize the distribution of points for each team and order the boxplots in ascending order based on the mean points scored by team:

import seaborn as sns

#calculate mean points by team
mean_by_team = df.groupby(['team'])['points'].mean().sort_values(ascending=True)

#create boxplots ordered by mean points (ascending)
sns.boxplot(x='team', y='points', data=df, order=mean_by_team.index)

Notice that the boxplots are ordered along the x-axis based on the mean points value by team in ascending order.

import seaborn as sns

#calculate mean points by team
mean_by_team = df.groupby(['team'])['points'].mean().sort_values(ascending=False)

#create boxplots ordered by mean points (descending)
sns.boxplot(x='team', y='points', data=df, order=mean_by_team.index)

The boxplots are now ordered along the x-axis based on the mean points value by team in descending order.

Note: To order the boxplots based on a different metric (e.g. the median), simply specify that metric after the groupby() function in the code above.

x