计算特定 bin 中元素的数量

sou*_*uli 2 python matplotlib histogram

我很好奇是否可以计算直方图中特定 bin 的元素数量,即 0-10 范围内的所有元素

你会怎么做?

例如 plt.hist(data, bins=[0, 10, 20, 30, 40, 50, 100]) 是否可以计算数据集中进入 bin 0-10 的所有元素

Max*_*Noe 6

Matplotlib 直方图返回每个 bin 的计数:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.uniform(0, 100, 1000)

counts, edges, plot = plt.hist(x, bins=[0, 10, 20, 50, 100])
print(counts)
print(counts[0]) # first bin
Run Code Online (Sandbox Code Playgroud)