Matplotlib 直方图 - 绘制大于给定值的值

Cat*_*aCM 5 python numpy matplotlib

我有以下直方图: 在此输入图像描述

它是用以下代码生成的:

import matplotlib.pyplot as plt
import numpy as num


treshold_file='false_alarms.txt'
with open(treshold_file, 'r') as f2:
    lines = f2.readlines()
data = [line.split() for line in lines]
data1 = num.array(data)
data2= data1.astype(float)
plt.hist((data2), alpha=0.4,bins=[100,110,120,130,   140,150,160,180,200,250,300,350,400])
plt.xlabel("treshold")
plt.ylabel("Frequency")
Run Code Online (Sandbox Code Playgroud)

我想为每个箱绘制大于或等于给定阈值的值的数量。

对于 bin 100,我想绘制 > 100 等的样本数。

And*_*eak 2

bar在构建必要的数据后,我会使用手动绘图:

import numpy as np
import matplotlib.pyplot as plt

# dummy data
data2 = np.random.randint(low=0, high=450, size=200)

bins = [100,110,120,130,140,150,160,180,200,250,300,350,400]
bincenters = (np.array(bins)[1:] + bins[:-1])/2
binwidths = np.diff(bins)
binvals = [np.sum(data2>=thresh) for thresh in bins[:-1]]

fig, ax = plt.subplots()
ax.bar(bincenters, binvals, width=binwidths, alpha=0.4,
       edgecolor=['darkblue'])
ax.set_xlabel('threshold')
ax.set_ylabel('occurences')
ax.autoscale('x', tight=True)

plt.show()
Run Code Online (Sandbox Code Playgroud)

结果:

条形图

该数组bins实际上是阈值列表 ( ndarray)。对于每个阈值,我们计算高于阈值的值的数量data2,这些是条形图的值,称为binvals。我们跳过最后一个索引,以获得输出中的正确维度。辅助数组bincenters包含每个箱的中点(通过取两个相应边的平均值)。