0

我有一个数据集,它是 python 中的元组列表,如下所示:

dataSet = [(6.1248199999999997, 27), (6.4400500000000003, 4), (5.9150600000000004, 1), (5.5388400000000004, 38), (5.82559, 1), (7.6892199999999997, 2), (6.9047799999999997, 1), (6.3516300000000001, 76), (6.5168699999999999, 1), (7.4382099999999998, 1), (5.4493299999999998, 1), (5.6254099999999996, 1), (6.3227700000000002, 1), (5.3321899999999998, 11), (6.7402300000000004, 4), (7.6701499999999996, 1), (5.4589400000000001, 3), (6.3089700000000004, 1), (6.5926099999999996, 2), (6.0003000000000002, 5), (5.9845800000000002, 1), (6.4967499999999996, 2), (6.51227, 6), (7.0302600000000002, 1), (5.7271200000000002, 49), (7.5311300000000001, 7), (5.9495800000000001, 2), (5.1487299999999996, 18), (5.7637099999999997, 6), (5.5144500000000001, 44), (6.7988499999999998, 1), (5.2578399999999998, 1)]

元组的第一个元素是能量,第二个元素是计数器,有多少传感器受到影响。

我想创建一个直方图来研究受影响传感器的数量与能量之间的关系。我对matplotlib(和python)很陌生,但这是我到目前为止所做的:

import math
import matplotlib.pyplot as plt

dataSet = [(6.1248199999999997, 27), (6.4400500000000003, 4), (5.9150600000000004, 1), (5.5388400000000004, 38), (5.82559, 1), (7.6892199999999997, 2), (6.9047799999999997, 1), (6.3516300000000001, 76), (6.5168699999999999, 1), (7.4382099999999998, 1), (5.4493299999999998, 1), (5.6254099999999996, 1), (6.3227700000000002, 1), (5.3321899999999998, 11), (6.7402300000000004, 4), (7.6701499999999996, 1), (5.4589400000000001, 3), (6.3089700000000004, 1), (6.5926099999999996, 2), (6.0003000000000002, 5), (5.9845800000000002, 1), (6.4967499999999996, 2), (6.51227, 6), (7.0302600000000002, 1), (5.7271200000000002, 49), (7.5311300000000001, 7), (5.9495800000000001, 2), (5.1487299999999996, 18), (5.7637099999999997, 6), (5.5144500000000001, 44), (6.7988499999999998, 1), (5.2578399999999998, 1)]

binWidth = .2
binnedDataSet = []
#create another list and append the "binning-value"
for item in dataSet:
    binnedDataSet.append((item[0], item[1], math.floor(item[0]/binWidth)*binWidth))

energies, sensorHits, binnedEnergy = [[q[i] for q in binnedDataSet] for i in (0,1,2)]
plt.plot(binnedEnergy, sensorHits, 'ro')
plt.show()

到目前为止,这很有效(尽管它甚至看起来不像直方图;-) 但是可以),但是现在我想计算每个 bin 的平均值并附加一些误差线。

有什么办法呢?我查看了 matplotlib 的直方图示例,但它们都使用将被计数的一维数据,所以你会得到一个频谱……这不是我真正想要的。

4

1 回答 1

1

我对你正在尝试做的事情感到有些困惑,但我认为这(一阶)会做我认为你想要的:

bin_width = .2
bottom = 5.0
top = 8.0

binned_data = [0.0] * int(math.ceil(((top - bottom) / bin_width)))
binned_count = [0] * int(math.ceil(((top - bottom) / bin_width)))
n_bins = len(binned_data)
for E, cnt in dataSet:
    if E < bottom or E > top:
        print 'out of range'
        continue
    bin_id = int(math.floor(n_bins * (E - bottom) / (top - bottom)))
    binned_data[bin_id] += cnt
    binned_count[bin_id] += 1

binned_avergaed_data = [C_sum / hits if hits > 0 else 0 for C_sum, hits in zip(binned_data, binned_count)]

bin_edges = [bottom + j * bin_width for j in range(len(binned_data))]

plt.bar(bin_edges, binned_avergaed_data, width=bin_width)

我还建议研究一下numpy,这样写起来会更简单。

于 2013-03-22T19:19:31.807 回答