python - 使用 fit=stats.norm 在 sns.distplot 中将 y 轴更改为百分比
问题描述
我正在绘制一系列高斯分布,据我了解,y 轴是密度而不是概率。有没有办法将其更改为百分比或概率?我的代码如下:
import seaborn as sns
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import norm
x = np.random.normal(size=500) * 0.1
ax = plt.figure()
sns.distplot(x, bins=15, kde =False, fit=norm, color='lightseagreen', fit_kws={"color":"lightseagreen"}, hist_kws=dict(alpha=0.7))
解决方案
您可以尝试手动定义箱,然后绘制直方图,这给你计数:
import seaborn as sns
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import norm
x = np.random.normal(size=500) * 0.1
fig, ax = plt.subplots()
nbins = 20
xlen = np.linspace(-0.4,0.4,nbins)
binwidth = xlen[1] - xlen[0]
sns.distplot(x, bins=xlen, kde =False, color='lightseagreen',
fit_kws={"color":"lightseagreen"}, hist_kws=dict(alpha=0.7),ax=ax)
然后拟合正态分布,使用 cdf 的差值获得这些 bin 内的概率,然后按比例缩小 y-ticks:
mu, std = norm.fit(x)
p = np.diff(norm.cdf((xlen-mu)/std))
ax.plot(xlen[:(len(xlen)-1)]+binwidth/2,p*len(x),color='lightseagreen')
ax.set_yticklabels(ax.get_yticks()/len(x))
推荐阅读
- angular - 我如何使用角度上传多张图片
- javascript - 找到唯一 ID 时更新或替换其他工作表中的行数据 - Google Appscript
- javascript - TypeError:无法读取未定义的属性“地图”,传递道具时出现问题
- sql - 通过使用 SQL Server 2008 给出默认的月份日期来计算日期
- javascript - 如何在数组javascript中添加对象
- augmented-reality - 模型不在标记 ARjs 之上
- javascript - 树图高图的不同父组的多个颜色轴
- oracle - dbeaver 表格编辑受阻,怎么办?
- flutter - Flutter - 如何将日期列表传递给 showDatePicker 中的 selectableDayPredicate?
- amazon-web-services - 我在 AWS ECS 上部署了一个反应应用程序并购买了一个自定义域,但我无法在域名前使用“www”访问它