python distplot 图_Python可视化23 |seaborn.distplot公司单变量分布图(直方图|核密度图),23seaborndistplot...
本文介紹seaborn.distplot繪制
單變量分布圖
;
之前文章介紹的
核密度圖
(kernel density estimate (KDE))以及
二者結(jié)合圖。
目錄
本文內(nèi)容概要
直方圖
核密度圖
直方圖結(jié)合核密度圖
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
整合了如下三個(gè)函數(shù):
matplotlib中的直方圖hist(默認(rèn)繪制直方圖)
seaborn.kdeplot()
seaborn.rugplot()
scipy.stats
數(shù)據(jù)準(zhǔn)備
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
x=np.random.randn(100)#造一個(gè)shape為(100,),服從正態(tài)分布的對(duì)象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,#默認(rèn)繪制直方圖,詳細(xì)參考plt.hist
kde=False,
color="#098154")#修改柱子顏色
修改直方圖hist中箱子數(shù)bins
plt.figure(dpi=120)
sns.set(style='dark')
sns.set_style("dark", {"axes.facecolor": "#e9f3ea"})
g=sns.distplot(x,
hist=True,
bins=15,#修改箱子個(gè)數(shù)
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}, #默認(rèn)為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',#設(shè)置外框線屬性
'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();
核密度曲線結(jié)合直方圖
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',#設(shè)置外框線屬性
},
color='#098154',
axlabel='Xlabel',#設(shè)置x軸標(biāo)題
)
fit參數(shù)
將數(shù)據(jù)與scipy.stats中的分布擬合,查看數(shù)據(jù)服從何種分布,更多可參考:https://docs.scipy.org/doc/scipy/reference/stats.html
from scipy.stats import norm#導(dǎo)入正態(tài)分布
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',
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python distplot 图_Python可视化23 |seaborn.distplot公司单变量分布图(直方图|核密度图),23seaborndistplot...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python课程思路_学习python课
- 下一篇: 40岁以上的人有福了,满足这个条件,一年