如何标准化直方图

发布于 2024-11-28 11:14:35 字数 480 浏览 1 评论 0原文

我有这个直方图,它对等对数间隔的容器中的数组“d”进行计数。

 max_val=np.log10(max(d))
 min_val=np.log10(min(d))
 logspace = np.logspace(min_val, max_val, 50) 


 hist(d,bins=logspace,label='z='+str(redshift),histtype='step')
 show()

问题是我希望将其标准化,以便面积为一。使用选项 Normed=True 我没有得到结果,这可能是因为我使用的是对数箱。因此我尝试用这种方式标准化直方图:

 H=hist(d,bins=logspace,label='z='+str(redshift),histtype='step')
 H_norm=H[0]/my_norm_constant

但是我不知道如何绘制 H_norm 与 bin 的关系图

I have this histogram which counts the array "d" in equally log-spaced bins.

 max_val=np.log10(max(d))
 min_val=np.log10(min(d))
 logspace = np.logspace(min_val, max_val, 50) 


 hist(d,bins=logspace,label='z='+str(redshift),histtype='step')
 show()

The problem is that I want it to be normalized so as the area is one. Using the option Normed=True I didn't get the result, it might be due to fact that I'm using logarithmic bins. Therefore I tried normalizing the histogram in this way:

 H=hist(d,bins=logspace,label='z='+str(redshift),histtype='step')
 H_norm=H[0]/my_norm_constant

But then I don't know how to plot H_norm versus the bins

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半山落雨半山空 2024-12-05 11:14:35

我尝试了normed=True,面积为1:

from pylab import *
d = np.random.normal(loc=20, size=10000)
max_val=np.log10(max(d))
min_val=np.log10(min(d))
logspace = np.logspace(min_val, max_val, 50) 


r = hist(d,bins=logspace,histtype='step', normed=True)
print "area":, sum(np.diff(r[1])*r[0])

您可以运行代码并检查输出吗?如果不是 1,请检查您的 numpy 版本。运行代码时收到此警告消息:

C:\Python26\lib\site-packages\matplotlib\axes.py:7680: UserWarning:
此版本修复了 NumPy 直方图中的标准化错误
1.5版本之前的函数,出现不一致
箱宽度。返回和绘制的值现在是密度:
n / (N * bin 宽度),
其中 n 是 bin 计数,N 是点总数。

自己绘制图表:

step(r[1][1:], r[0]/my_norm_constant)

I tried normed=True, and the area is 1:

from pylab import *
d = np.random.normal(loc=20, size=10000)
max_val=np.log10(max(d))
min_val=np.log10(min(d))
logspace = np.logspace(min_val, max_val, 50) 


r = hist(d,bins=logspace,histtype='step', normed=True)
print "area":, sum(np.diff(r[1])*r[0])

can you run the code, and check the output. If it is not 1, check your numpy version. I got this warning message when run the code:

C:\Python26\lib\site-packages\matplotlib\axes.py:7680: UserWarning:
This release fixes a normalization bug in the NumPy histogram
function prior to version 1.5, occuring with non-uniform
bin widths. The returned and plotted value is now a density:
n / (N * bin width),
where n is the bin count and N the total number of points.

to plot the graph yourself:

step(r[1][1:], r[0]/my_norm_constant)
简美 2024-12-05 11:14:35

这使用常见的归一化,将 bin 高度归一化为 1,无论 bin 宽度如何。

import matplotlib
import numpy as np

x = [0.1,0.2,0.04,0.05,0.05,0.06,0.07,0.11,0.12,0.12,0.1414,\
     0.1415,0.15,0.12,0.123,0,0.14,0.145,0.15,0.156,0.12,0.15,\
     0.156,0.166,0.151,0.124, 0.12,0.124,0.12,0.045,0.124]

weights = np.ones_like(x)/float(len(x))
p=plt.hist(x,
    bins=4,
    normed=False, 
    weights=weights,
    #histtype='stepfilled',
    color=[0.1,0.4,0.3]
)

plt.ylim(0,1)
plt.show()

生成的直方图:

This uses the common normalization which normalizes bin height to add up to 1 irrespective of bin width.

import matplotlib
import numpy as np

x = [0.1,0.2,0.04,0.05,0.05,0.06,0.07,0.11,0.12,0.12,0.1414,\
     0.1415,0.15,0.12,0.123,0,0.14,0.145,0.15,0.156,0.12,0.15,\
     0.156,0.166,0.151,0.124, 0.12,0.124,0.12,0.045,0.124]

weights = np.ones_like(x)/float(len(x))
p=plt.hist(x,
    bins=4,
    normed=False, 
    weights=weights,
    #histtype='stepfilled',
    color=[0.1,0.4,0.3]
)

plt.ylim(0,1)
plt.show()

resulting histogram plot:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文