seaborn displot noting entire data

81 Views Asked by At

enter image description here

sns.displot(df,x='Age', col='Sex',kind='kde',multiple="stack")

I want to express the gender distribution of all passengers with Titanic data. How do I display the total data of Male+Female on each gender graph?

sns.displot(df,x='Age', col='Sex',kind='kde',multiple="stack")
2

There are 2 best solutions below

0
On

Note that this sort of thing is substantially easier in the new objects interface:

titanic = sns.load_dataset("titanic")
(
    so.Plot(titanic, "age")
    .facet(col="sex")
    .add(so.Line(color="r"), so.KDE(), col=None)
    .add(so.Area(), so.KDE())

)

enter image description here

0
On

The approach I'd take here with seaborn's plotting functions is to draw the faceted plot akin to your example and then iterate over the axes to add the background line on each one:

titanic = sns.load_dataset("titanic")
g = sns.displot(titanic, x="age", col="class", kind="kde", fill=True)
for ax in g.axes.flat:
    sns.kdeplot(data=titanic, x="age", fill=False, ax=ax, color="r")

enter image description here

If you add common_norm=False to the displot call, every distribution will integrate to 1 and it may be easier to compare the shapes (this looks a little bit more like your cartoon):

enter image description here