Is there a way to add the mean and median to Seaborn's displot
?
penguins = sns.load_dataset("penguins")
g = sns.displot(
data=penguins, x='body_mass_g',
col='species',
facet_kws=dict(sharey=False, sharex=False)
)
Based on Add mean and variability to seaborn FacetGrid distplots, I see that I can define a FacetGrid
and map a function. Can I pass a custom function to displot
?
The reason for trying to use displot
directly is that the plots are much prettier out of the box, without tweaking tick label size, axis label size, etc. and are visually consistent with other plots I am making.
def specs(x, **kwargs):
ax = sns.histplot(x=x)
ax.axvline(x.mean(), color='k', lw=2)
ax.axvline(x.median(), color='k', ls='--', lw=2)
g = sns.FacetGrid(data=penguins, col='species')
g.map(specs,'body_mass_g' )
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…