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

Question:

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?

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

Asked By: missingfours

||

Answers:

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

Answered By: mozway
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.