How do you display the mean value on a boxplot?

The mean value of a boxplot is displayed as a line in the middle of the box. It is calculated by taking the average of the set of data values. The line can be labeled with the mean or median value, depending on the preference. The median is the midpoint of the distribution and is the value that divides the data set in half, while the mean is the average of all the data points in the set.


You can use the showmeans argument to display the mean value in boxplots created using seaborn:

sns.boxplot(data=df, x='x_var', y='y_var', showmeans=True)

The following example shows how to use this syntax in practice.

Example: Display Mean Value on Seaborn Boxplot

Suppose we have 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

We can use the following code to create boxplots to visualize the distribution of points for each team:

import seaborn as sns

#create boxplot to visualize points distribution by team
sns.boxplot(data=df, x='team', y='points')

By default, the boxplots display the median value using a horizontal line inside each boxplot.

To display the mean value for each boxplot, you must specify showmeans=True:

import seaborn as sns

#create boxplot to visualize points distribution by team (and display mean values)
sns.boxplot(data=df, x='team', y='points', showmeans=True)

By default, seaborn uses green triangles to display the mean value for each boxplot.

To customize the appearance of the mean value, feel free to use the meanprops argument:

import seaborn as sns

#create boxplot to visualize points distribution by team
sns.boxplot(data=df, x='team', y='points', showmeans=True,
            meanprops={'marker':'o',
                       'markerfacecolor':'white', 
                       'markeredgecolor':'black',
                       'markersize':'8'})

Feel free to play around with the values in the meanprops argument to modify the appearance of the mean values in the boxplots.

Note: You can find the complete documentation for the seaborn boxplot() function .

x