密度图也就是我们所说的直方图,可以反映数据的分布,

seaborn绘制直方图

  1. plt.figure()
  2. sns.distplot(df['age'],bins=50,color='blue')

密度图 - 图1

  1. sns.displot(x=df['age'],hue='survived',data=df)

密度图 - 图2

  • 这种图堆叠了颜色,增加了一个维度
  • 这种方法对二分类变量比较有效

matplotlib的直方图

  1. df_live = df[df['survived']==1]
  2. df_dead = df[df['survived']==0]
  3. plt.figure()
  4. ax1 = plt.subplot(2,1,1)
  5. ax1.hist(df_live['age'],color='blue',alpha=0.8)
  6. ax2 = plt.subplot(2,1,2)
  7. ax2.title.set_text('dead')
  8. ax2.set_xlabel('age')
  9. ax2.hist(df_dead['age'],color='red',alpha=0.8)

密度图 - 图3