gpt4 book ai didi

python - matplotlib.pyplot.hist 错误的规范属性

转载 作者:行者123 更新时间:2023-12-01 01:29:10 25 4
gpt4 key购买 nike

我正在创建在数据框中组织并按天分组的数据直方图。有时可能会出现数据完全为空的情况。因此,当我使用 normed = True 绘制直方图时属性,我期望有一个以 0 为中心且高度等于 1 的 bin。但是,我发现高度等于 bin 的数量。我怎样才能解决这个问题?我想用直方图来表示概率密度函数,所以最大值应该是1。

代码示例和输出:

plt.rcParams['figure.figsize'] = 10, 4
data = np.zeros((1000))
l = plt.hist(data,normed = True, bins = 100)

enter image description here

编辑:我现在看到该属性 normed已弃用。但是,如果我尝试使用属性 density ,我收到错误 AttributeError: Unknown property density

最佳答案

您看到的图是正确的,因为曲线下的面积(直方图/条形图)应该为 1。您的图中确实是这种情况。为了强调这一点,我在 x=0.01 处创建了一条垂直线,您会注意到条形的宽度确实是 0.01。由于条形的高度为 100,因此面积为 100 * 0.01 = 1。

plt.rcParams['figure.figsize'] = 10, 4
data = np.zeros((1000))
l = plt.hist(data,normed = True, bins = 100)
plt.axvline(0.01, lw=1)
plt.ylim(0, 150)

如果您使用密度=True作为

,也会发生同样的情况
l = plt.hist(data,density = True, bins = 100)

enter image description here

使用jdehesa的建议,以下按您的方式工作

l = plt.hist(data,density = True, bins=np.arange(-10, 11))

enter image description here

使用DavidG的建议基于this答案给出的高度为 1,但面积未标准化为 1。

weights = np.ones_like(data)/float(len(data))
l = plt.hist(data,weights=weights)

enter image description here

最后,如果您需要高度为 1、宽度为 1(因此面积 = 1)以及标准化面积,则可以使用单个 bin 作为

l = plt.hist(data, density=True, bins=1)
plt.xlim(-10, 10)

enter image description here

关于python - matplotlib.pyplot.hist 错误的规范属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53063231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com