Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data points from a histogram in Python

I made a histogram of the 'cdf' (cumulative distribution) of a function. The histogram is basically No. of counts vs. luminosity. Now, how do I extract data points from a histogram? I need actual values of Luminosities. I am using Matplotlib in Python, and any online book, example, tutorial etc is not helping.

l= [ss.gammainccinv(aa+1, u[i]) for i in a] #is the cdf function
plt.hist(l, 50, histtype= 'step', align='mid') #is the histogram
plt.show()

I am not sure if I should align the bins to the edges or the mid point, but all I need is a list of l's.

Any suggestions would be greatly appreciated!

like image 207
user3014593 Avatar asked Nov 21 '13 18:11

user3014593


People also ask

How do you get data from a histogram in Python?

The plt() function present in pyplot submodule of Matplotlib takes the array of dataset and array of bin as parameter and creates a histogram of the corresponding data values.

How do you make a frequency table from a histogram in Python?

To create a histogram in Python using Matplotlib, you can use the hist() function. This hist function takes a number of arguments, the key one being the bins argument, which specifies the number of equal-width bins in the range.


1 Answers

You've already got a list of ls, 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)
like image 91
askewchan Avatar answered Oct 25 '22 18:10

askewchan