Python可视化23|seaborn.distplot单变量分布图(直方图|核密度图)

Python可视化23|seaborn.distplot单变量分布图(直方图|核密度图)

本文介绍seaborn.distplot绘制单变量分布图直方图核密度图)。

本文内容概要

欢迎随缘关注@pythonic生物人

  • 直方图
  • ​核密度图
  • 直方图结合核密度图

目录

1、seaborn.distplot
数据准备
绘制直方图hist
修改直方图hist中箱子数bins 
直方图成箱方式 
绘制核密度曲线kernel density estimate (KDE)
seaborn.kdeplot绘制窄宽度核密度曲线 
bandwidth (bw),控制核密度曲线胖瘦
核密度曲线结合直方图
fit参数

seaborn.distplot简介

seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None, hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None, color=None, vertical=False, norm_hist=False, axlabel=None, label=None, ax=None)
seaborn.pydata.org/gene
整合了如下三个函数:
matplotlib中的直方图hist(默认绘制直方图)
seaborn.kdeplot()
seaborn.rugplot()
scipy.stats

数据准备

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
x=np.random.randn(100)#造一个shape为(100,),服从正态分布的对象x
print(x)
print(x.shape)

绘制直方图hist

plt.figure(dpi=120)
sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})#修改背景色
g=sns.distplot(x,
               hist=True,#默认绘制直方图,详细参考plt.hist
               kde=False,
               color="#098154")#修改柱子颜色

修改直方图hist中箱子数bins

plt.figure(dpi=120)
sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})
g=sns.distplot(x,
               hist=True,
               bins=15,#修改箱子个数
               kde=False,
               color="#098154")

直方图成箱方式

有4种方式:'bar,barstacked,step,stepfilled'。

sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})
for i in list('bar,barstacked,step,stepfilled'.split(',')):
    plt.figure(dpi=120)
    sns.distplot(x,
                 hist=True,
                 bins=15,
                 kde=False,
                 hist_kws={'histtype':'%s'%i}, #默认为bar,可选barstacked,step,stepfilled
                 color="#098154")
    plt.title("histtype="'%s'%i)
    plt.show()

绘制核密度曲线kernel density estimate (KDE)

plt.figure(dpi=120)
sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})
g=sns.distplot(x,
               hist=False,
               kde=True,#开启核密度曲线kernel density estimate (KDE)
               kde_kws={'linestyle':'--','linewidth':'1','color':'#098154',#设置外框线属性
                        'shade':True,#开启填充                        
                       },
              )

seaborn.kdeplot绘制窄宽度核密度曲线

plt.figure(dpi=120)
sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})#修改背景色
g=sns.kdeplot(x,
              shade=True,
              bw=0.15,#使用窄带宽
              color="#098154"
              )

bandwidth (bw),控制核密度曲线胖瘦

类似hist中bin size

plt.figure(dpi=120)
sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})
sns.kdeplot(x,shade=True, label="bw: defult")
sns.kdeplot(x, bw=.2, label="bw: 0.2")
sns.kdeplot(x, bw=2, label="bw: 2")
plt.legend();

核密度曲线结合直方图

plt.figure(dpi=120)
sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})
g=sns.distplot(x,
               hist=True,
               kde=True,#开启核密度曲线kernel density estimate (KDE)
               kde_kws={'linestyle':'--','linewidth':'1','color':'#c72e29',#设置外框线属性                                               
                       },
               color='#098154',
               axlabel='Xlabel',#设置x轴标题
               
              )

fit参数

将数据与scipy.stats中的分布拟合,查看数据服从何种分布,更多可参考:docs.scipy.org/doc/scip

from scipy.stats import norm#导入正态分布
plt.figure(dpi=120)
sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})
g=sns.distplot(x,
               hist=True,
               kde=False,
               kde_kws={'linestyle':'--','linewidth':'1','color':'#c72e29',                                              
                       },
               fit=norm,#
               color='#098154',)
​ 参考资料:seaborn.pydata.org/gene

本文结束,欢迎随缘关注@pythonic生物人

编辑于 2020-08-22 09:54