Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add a point to seaborn stripplot that signifies mean for each category

I am using seaborn to create a stripplot for three conditions. The example data look like:

    df = pd.DataFrame(
        {
             'bill': [50, 45, 33, 23, 22, 34, 54, 22, 54, 76], 
             'day': ['sat', 'sat', 'sat', 'sat', 'sat', 'sun', 'sun', 'sun', 'sun', 'sun'], 
             'tip': ['yes', 'no', 'yes', 'no', 'yes', 'no', 'yes', 'no', 'yes', 'no']
             }
             )

The seaborn plot:

sns.stripplot(x='day', y='bill', data=df, jitter=True, hue='tip', palette='deep', dodge=True)

How can I plot a point in each category that signifies the mean of that category?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried adapting this [code][1] , but this created average for day but not separate averages for day/tip.

Thanks in advance!

[1]: https://stackoverflow.com/questions/67481900/how-to-add-a-mean-line-to-a-seaborn-stripplot-or-swarmplot#:~:text=import%20seaborn%20as,p)%0Aplt.show()

>Solution :

Maybe overlay a pointplot?

import seaborn as sns
sns.stripplot(x='day', y='bill', data=df, jitter=True, hue='tip', palette='deep', dodge=True)
sns.pointplot(x='day', y='bill', data=df, hue='tip', palette='deep', dodge=True, linestyles='', errorbar=None)

Output:

enter image description here

You can cheat a bit to have a more meaningful legend:

import seaborn as sns
sns.stripplot(x='day', y='bill', data=df, jitter=True, hue='tip', palette='deep', dodge=True)
sns.pointplot(x='day', y='bill', data=df.assign(tip=df['tip']+' (avg)'),
              hue='tip', palette='deep',
              dodge=True, linestyles='', errorbar=None)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading