gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:15 25 4
gpt4 key购买 nike

我有以下直方图: enter image description here

它是用这段代码生成的:

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")

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

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

最佳答案

在构建必要的数据后,我会使用手动 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()

结果:

bar plot

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

关于python - Matplotlib 直方图 - 绘制大于给定值的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34909446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com