具有误差条的matplotlib直方图的数据处理

时间:2013-03-20 19:11:10

标签: matplotlib histogram

我有一个数据集,这是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的直方图示例,但它们都使用了将被计算的一维数据,因此您得到了一个频谱......这不是我想要的。

1 个答案:

答案 0 :(得分: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,这样可以更容易编写。