带有错误条的matplotlib直方图的数据处理

带有错误条的matplotlib直方图的数据处理,matplotlib,histogram,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.516869999999999

我有一个数据集,它是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()
到目前为止,这是可行的(虽然它甚至不像直方图;-),但还行),但现在我想计算每个箱子的平均值,并附加一些错误条


怎么做?我查看了matplotlib的直方图示例,但它们都使用一维数据,这些数据将被计数,因此您可以得到频谱…这并不是我真正想要的。

我对您正试图做的有点困惑,但我认为(首先)这将实现我认为您想要的:

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)
bin_宽度=.2
底部=5.0
top=8.0
binned_data=[0.0]*int(math.ceil(((顶部-底部)/bin_宽度)))
binned_count=[0]*int(math.ceil(((顶部-底部)/bin_宽度)))
n\u bins=len(装箱数据)
对于E,数据集中的cnt:
如果E<底部或E>顶部:
打印“超出范围”
持续
bin_id=int(数学层(n_bin*(E-bottom)/(top-bottom)))
binned_数据[bin_id]+=cnt
binned_计数[bin_id]+=1
binned_avergaed_data=[C_sum/hits if hits>0否则C_sum为0,hits in zip(binned_data,binned_count)]
bin_边=[底部+j*范围内j的bin_宽度(len(binned_数据))]
plt.bar(箱边,箱平均数据,宽度=箱宽)
我还建议研究一下
numpy
,这将使编写更简单