Home » How to Order Boxplots on x-axis in Seaborn

How to Order Boxplots on x-axis in Seaborn

by Tutor Aspire

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.

To display the boxplots in descending order, simply specify ascending=False within the sort_values() function:

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.

Additional Resources

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

How to Remove Outliers from a Seaborn Boxplot
How to Create a Boxplot of Multiple Columns in Seaborn

You may also like