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'})
The mean values are now displayed as white circles with black outlines.
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 here.
Additional Resources
The following tutorials explain how to perform other common functions in seaborn:
How to Control Colors in Seaborn Boxplot
How to Remove Outliers from a Seaborn Boxplot
How to Order Boxplots on x-axis in Seaborn