'Plot density chart with Age and Sex
This is the easiest way I found for a plot for two variables with Seaborn (Age and Sex):
M = df[df["Sex"]=="male"]
F = df[df["Sex"]=="female"]
X1 = M["Age"].dropna()
X2 = F["Age"].dropna()
L1=sns.kdeplot(X1, shade=True, label="male", color="orangered", alpha=0.4)
L2=sns.kdeplot(X2, shade=True, label="female",color='royalblue', alpha=0.4)
How can I make this chart with an improved code?
Thanks.
Solution 1:[1]
Here is a shortcode to achieve the same thing
sns.kdeplot(df["Age"], shade=True, hue=df["Sex"], color="orangered",
alpha=0.4)
Solution 2:[2]
You should be able to use the 'hue' option to separate sub-classes of attributes like sex. So you can now define the plot in one line:
plot = sns.PairGrid(df["Age"].dropna(), hue="Sex", **kwargs)
plot = plot.map(sns.kdeplot, **kwargs)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |

