How can I display the mean value on a boxplot using Seaborn? 2

How can I display the mean value on a boxplot using Seaborn?

Seaborn is a data visualization library in Python that allows users to create informative and visually appealing plots. One of its features is the ability to display the mean value on a boxplot. A boxplot is a graphical representation of numerical data through quartiles, median, and outliers. To display the mean value on a boxplot using Seaborn, the user can use the “showmeans” parameter in the boxplot function. This will add a marker or line at the mean value of the data, providing additional insight and understanding of the data distribution. By incorporating the mean value into a boxplot, it becomes a more comprehensive and informative visualization tool for data analysis.

Seaborn: Display Mean Value on Boxplot


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 .

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

Cite this article

stats writer (2024). How can I display the mean value on a boxplot using Seaborn?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-display-the-mean-value-on-a-boxplot-using-seaborn/

stats writer. "How can I display the mean value on a boxplot using Seaborn?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-display-the-mean-value-on-a-boxplot-using-seaborn/.

stats writer. "How can I display the mean value on a boxplot using Seaborn?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-display-the-mean-value-on-a-boxplot-using-seaborn/.

stats writer (2024) 'How can I display the mean value on a boxplot using Seaborn?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-display-the-mean-value-on-a-boxplot-using-seaborn/.

[1] stats writer, "How can I display the mean value on a boxplot using Seaborn?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I display the mean value on a boxplot using Seaborn?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top