Get Data Points From A Histogram In Python
Solution 1:
You've already got a list of l
s, so I'm not sure what you mean by that last statement, so maybe I've misinterpreted the question.
To get the values from a histogram, plt.hist
returns them, so all you have to do is save them.
If you don't save them, the interpreter just prints the output:
In [34]: x = np.random.rand(100)
In [35]: plt.hist(x)
Out[35]:
(array([ 11., 9., 10., 6., 8., 8., 10., 10., 11., 17.]),
array([ 0.00158591, 0.100731 , 0.19987608, 0.29902116, 0.39816624,
0.49731133, 0.59645641, 0.69560149, 0.79474657, 0.89389165,
0.99303674]),
<a list of 10 Patch objects>)
So, to save them, do:
counts, bins, bars = plt.hist(x)
Solution 2:
You can also retrieve info from an already plotted histogram. For example:
x = np.random.rand(100)
plt.hist(x)
ax = plt.gca() # get axis handle
You can access to each individual bar using patches
. Then .get_xy()
will give you the x-y coordiate of a single bar (lower left corner), .get_width()
will give width of the bar, and .get_height()
will give the height of the bar (which is probably what you want).
p = ax.patches # There are 10 patches
p[0].get_xy()
>> (0.011714084185001972, 0.0)
p[0].get_width()
>>0.09871329223828645
p[0].get_height()
>>8.0
So to retrieve all the heights:
heights = [patch.get_height() for patch in p]
And to reproduce the histogram:
plt.bar(range(len(heights)), heights)
Solution 3:
if you want to get the assignments to bins, You can do it in the following way as explained here Which elements of list go into which histogram bins?
nbins=7
# Some example data
A = np.random.randint(0, 10, 10)
B = np.random.rand(10)
counts, binEdges=np.histogram(A,bins=nbins)
>>> binEdges, np.digitize(A, binEdges)
array([ 0. , 1.28571429, 2.57142857, 3.85714286, 5.14285714,
6.42857143, 7.71428571, 9. ])
Post a Comment for "Get Data Points From A Histogram In Python"