小编Ben*_*Ben的帖子

如何在直方图bin中获取数据

我想得到一个直方图箱中包含的数据列表.我正在使用numpy和Matplotlib.我知道如何遍历数据并检查bin边缘.但是,我想对2D直方图这样做,而执行此操作的代码相当丑陋.numpy有没有任何结构可以让这更容易?

对于1D情况,我可以使用searchsorted().但逻辑并没有那么好,我真的不想在没有必要时对每个数据点进行二进制搜索.

大多数令人讨厌的逻辑是由于bin边界区域.所有区域都有这样的边界:[左边缘,右边缘].除了最后一个bin,它有一个像这样的区域:[left edge,right edge].

以下是1D案例的示例代码:

import numpy as np

data = [0, 0.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 3]

hist, edges = np.histogram(data, bins=3)

print 'data =', data
print 'histogram =', hist
print 'edges =', edges

getbin = 2  #0, 1, or 2

print '---'
print 'alg 1:'

#for i in range(len(data)):
for d in data:
    if d >= edges[getbin]:
        if (getbin == len(edges)-2) or d < edges[getbin+1]:
            print 'found:', d
        #end if
    #end if
#end for

print …
Run Code Online (Sandbox Code Playgroud)

python numpy matplotlib histogram

18
推荐指数
2
解决办法
3万
查看次数

标签 统计

histogram ×1

matplotlib ×1

numpy ×1

python ×1