如何在加权直方图中获取特定bin的数据点?

问题描述

我在python中有一个加权直方图,我希望拥有特定bin的所有数据点。

我用它来绘制直方图:

c,n,x=plt.hist(e,bins=50,range=(-500,-400),weights=p,color='blue')

ep都有13万个数据点。 我喜欢获取特定bin的所有数据点(让我们说一下-450)。

解决方法

您可以尝试如下操作:

c,n,x=plt.hist(e,bins=50,range=(-500,-400),weights=p,color='blue')
ind = np.where(n == -450)[0][0]
print(c[ind])

示例:

np.random.seed(0)

#data
mu = -450  # mean of distribution
sigma = 50  # standard deviation of distribution
x = mu + sigma * np.random.randn(10000)

num_bins = 50

# the histogram of the data
n,bins,patches = plt.hist(x,num_bins,density=True,-400))

ind = np.where(bins == -450)[0][0]
print(n[ind])

Output->

0.011885780547905494

希望,这可能会有所帮助!