Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python histogram with points and error bars

I want to plot a histogram with points and error bars. I do not want bar or step histograms. Is this possible? Google has not helped me, I hope you can. Also it should not be normalized. Thanks!

like image 269
Abhinav Kumar Avatar asked Nov 01 '22 13:11

Abhinav Kumar


1 Answers

Assuming you're using numpy and matplotlib, you can get the bin edges and counts using np.histogram(), then use pp.errorbar() to plot them:

import numpy as np
from matplotlib import pyplot as pp

x = np.random.randn(10000)
counts,bin_edges = np.histogram(x,20)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2.
err = np.random.rand(bin_centres.size)*100
pp.errorbar(bin_centres, counts, yerr=err, fmt='o')

pp.show()

enter image description here

I'm not sure what you mean by 'normalized', but it would be easy to, for example, divide the counts by the total number of values so that the histogram sums to 1.

The bigger question for me is what the errorbars would actually mean in the context of a histogram, where you're dealing with absolute counts for each bin.

like image 162
ali_m Avatar answered Nov 15 '22 05:11

ali_m