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.
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:
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)
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.
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:
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)