如何从现有bin值绘制直方图

时间:2018-09-18 04:12:34

标签: python json matplotlib histogram

我发现的所有使用Border绘制直方图的示例都显示了如何使用matplotlib函数绘制 unbinned 数据的直方图。如何绘制以 binned 数据开头的直方图。

具体而言,这是个完整的问题。 我有一个hist文件

json

{ "axes": [ { "nbins": 50, "range": [ 0.0, 1.0 ] } ], "bins": [ [ [ 0.03912266159779804, 2.4253202838786034e-07 ], 5532865 ], [ [ 0.03916990070252777, 4.895032330300466e-07 ], 5431456 ], [ [ 0.03898489463963174, 4.205432148046357e-07 ], 5321885 ], [ [ 0.037217159462905665, 2.796781402951966e-07 ], 5215454 ], [ [ 0.037591952220843425, 3.7157817818538036e-07 ], 5152861 ], [ [ 0.03640064749941381, 3.429133584283686e-07 ], 5116330 ], [ [ 0.03545469456846452, 6.236972626535147e-07 ], 5078941 ], [ [ 0.03567967996123658, 5.850877600595089e-07 ], 5038978 ], [ [ 0.03664350544390463, 2.6838483775984445e-07 ], 5009234 ], [ [ 0.03543507077587162, 3.319294632414073e-07 ], 4974721 ], [ [ 0.03537148076591839, 4.0269995315009494e-07 ], 4931893 ], [ [ 0.03478870277954808, 3.1711886205012627e-07 ], 4891786 ], [ [ 0.033864757004235656, 2.6868299634358844e-07 ], 4851690 ], [ [ 0.03428355267988012, 5.341109499844148e-07 ], 4801015 ], [ [ 0.03403395347207946, 2.5288200846762164e-07 ], 4760079 ], [ [ 0.033613239317709286, 2.1708231648080382e-07 ], 4704358 ], [ [ 0.03277840091277388, 2.0527513043163183e-07 ], 4652398 ], [ [ 0.03160892627026973, 2.183991948510151e-07 ], 4605129 ], [ [ 0.031538833244624406, 2.444679281977994e-07 ], 4543887 ], [ [ 0.03190446224434107, 2.429910009839857e-07 ], 4496607 ], [ [ 0.03241097134419747, 4.7225070321727006e-07 ], 4463153 ], [ [ 0.030148277612016283, 4.1542625940340425e-07 ], 4441211 ], [ [ 0.031175420934497718, 1.9104643541211688e-07 ], 4409245 ], [ [ 0.029838668257704842, 1.8894682212226968e-07 ], 4361187 ], [ [ 0.030212171384252064, 1.8894789156090747e-07 ], 4298157 ], [ [ 0.029523088683353363, 5.720761674504702e-07 ], 4222940 ], [ [ 0.027569612339860272, 3.4163680780786545e-07 ], 4137497 ], [ [ 0.02705999608770834, 1.7675692784960427e-07 ], 4043101 ], [ [ 0.027092508630256265, 3.059147357589793e-07 ], 3936603 ], [ [ 0.025520005653155698, 2.9155690929880814e-07 ], 3821513 ], [ [ 0.025075543788955813, 1.2962491061369452e-07 ], 3686948 ], [ [ 0.023501885386189723, 1.633089763898124e-07 ], 3558060 ], [ [ 0.02319278863781888, 1.4717090690523133e-07 ], 3397555 ], [ [ 0.02246957607921931, 2.6205756706209384e-07 ], 3227434 ], [ [ 0.02177163302477613, 9.805940747435215e-08 ], 3042162 ], [ [ 0.021447684447593745, 8.130626024719122e-08 ], 2852311 ], [ [ 0.020180402151551657, 8.46456889191387e-08 ], 2688089 ], [ [ 0.018484791515543934, 5.325662194453329e-08 ], 2519457 ], [ [ 0.016700717096649637, 6.050765025320019e-08 ], 2345115 ], [ [ 0.015929322854177978, 5.0953284820774626e-08 ], 2151772 ], [ [ 0.014118937342224115, 4.06366038569279e-08 ], 1935428 ], [ [ 0.013577901278784827, 2.7159002804458046e-08 ], 1738468 ], [ [ 0.01301532640962796, 2.1745148207135212e-08 ], 1576686 ], [ [ 0.012150411926165821, 1.7015278009418007e-08 ], 1484663 ], [ [ 0.011791276170920642, 1.9663332742339892e-08 ], 1427183 ], [ [ 0.011356843311177617, 2.3150396622524826e-08 ], 1400035 ], [ [ 0.011742382443851514, 1.8228381946768994e-08 ], 1393307 ], [ [ 0.0117014991872856, 1.5232934897253802e-08 ], 1414013 ], [ [ 0.0119729712509594, 1.8410870864962435e-08 ], 1461896 ], [ [ 0.012871377959299282, 1.33946434676272e-08 ], 1540758 ] ] } 数组的元素布局如下:

"bins"

事件数未用于绘制。

我想要的图形表示与此类似: enter image description here

1 个答案:

答案 0 :(得分:1)

由于您已经对数据进行分箱,因此只需使用pyplot.errorbar或将yerr kwarg添加到条形图中即可

from matplotlib import pyplot as plt

plt.errorbar(
    [n/len(bins) for n, x in enumerate(bins)],
    [x[0][0] for x in bins],
    yerr = [x[0][1]**0.5 for x in bins],
    marker='_', fmt='.')

plt.bar(
    [n/len(bins) for n, x in enumerate(bins)],
    [x[0][0] for x in bins],
    yerr = [x[0][1]**0.5 for x in bins],
    width=1/80)

plt.errorbar plt.bar(x, y, width, **yerr**