python distplot 图_Python可视化23 |seaborn.distplot公司单变量分布图(直方图|核密度图),23seaborndistplot...

本文介绍seaborn.distplot绘制

单变量分布图

之前文章介绍的

核密度图

(kernel density estimate (KDE))以及

二者结合图。

目录

本文内容概要

直方图

核密度图

直方图结合核密度图

1、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)

http://seaborn.pydata.org/generated/seaborn.distplot.html#seaborn.distplot

整合了如下三个函数:

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中的分布拟合,查看数据服从何种分布,更多可参考:https://docs.scipy.org/doc/scipy/reference/stats.html

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',

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值