How to get data in a histogram bin

后端 未结 2 725
鱼传尺愫
鱼传尺愫 2021-02-01 20:22

I want to get a list of the data contained in a histogram bin. I am using numpy, and Matplotlib. I know how to traverse the data and check the bin edges. However, I want to d

2条回答
  •  粉色の甜心
    2021-02-01 21:00

    how about something like:

    data = numpy.array([0, 0.5, 1.5, 1.5, 1.5, 2.5, 2.5, 2.5, 3])
    
    hist, edges = numpy.histogram(data, bins=3)
    
    for l, r in zip(edges[:-1], edges[1:]):
       print(data[(data > l) & (data < r)]) 
    

    Out:

    [ 0.5]
    [ 1.5  1.5  1.5]
    [ 2.5  2.5  2.5]
    

    with a bit of code to handle the edge cases.

提交回复
热议问题