密度图也就是我们所说的直方图,可以反映数据的分布,
seaborn绘制直方图
plt.figure()sns.distplot(df['age'],bins=50,color='blue')
sns.displot(x=df['age'],hue='survived',data=df)
- 这种图堆叠了颜色,增加了一个维度
- 这种方法对二分类变量比较有效
matplotlib的直方图
df_live = df[df['survived']==1]df_dead = df[df['survived']==0]plt.figure()ax1 = plt.subplot(2,1,1)ax1.hist(df_live['age'],color='blue',alpha=0.8)ax2 = plt.subplot(2,1,2)ax2.title.set_text('dead')ax2.set_xlabel('age')ax2.hist(df_dead['age'],color='red',alpha=0.8)
