python - 计算特定 bin 中的元素数

标签 python matplotlib histogram

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

你会怎么做?

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

最佳答案

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

关于python - 计算特定 bin 中的元素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55482071/

相关文章:

python - 根据列定位从两个数据框中获取数字的百分比变化

python - 在 64 位 Windows 上安装 NumPy 和 SciPy(使用 Pip)

python - odeint 中的参数数组

python从两个目录文件中导入一个

python - 在 matplotlib 图中突出显示具有特定 x 值范围的子序列

matplotlib - 尝试分割图像颜色时出错 : numpy. ndarray' 对象没有属性 'mask'

python - key 错误 : '[datetime.time(12, 19, 29) not in index'

gnuplot,将 y 轴分成两部分

javascript - Highcharts 柱系列 : Centering categorized x-axis labels

r - 几何直方图 : What is the default origin of the first bin?