How to add mean into boxplot visualization?
Asked Answered
F

2

12

I was able to visualize my data in a boxplot using Seaborn.

sns.boxplot( x=df['Score'].astype('float'), y=df['Group'] )

The visualization shows me: all four quartiles, lower and upper whisker, and some outliers. How can I also add the Mean line into the boxplots? See current visualization (without mean).

enter image description here

Thanks!

Fendley answered 24/9, 2018 at 21:4 Comment(1)
Possible duplicate of Show mean in the box plot in python?Cody
F
29

I just figured it out. The code works like this:

sns.boxplot(x=df['Score'].astype('float'), y=df['Group'],showmeans=True )
Fendley answered 24/9, 2018 at 21:25 Comment(2)
i find more elegant if you throw meanline=True into the mix, that is, inside sns.boxplot(....Agraphia
Note: showmeans is a matplotlib argument.Isagogics
F
1

This is a bit late but I thought I'd add how to format the mean value. By default, seaborn uses green triangles to display the mean value for each boxplot.

sns.boxplot(x=df['Score'].astype('float'), y=df['Group'],showmeans=True, meanprops={'marker':'o','markerfacecolor':'white','markeredgecolor':'black','markersize':'8'})

The mean values will be displayed as white circles with black outlines.

You can play around with the values in the meanprops argument to modify the appearance of the mean values in the boxplots.

Frankhouse answered 2/5 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.