Products
Home
Get data points from a histogram in Ask Question
PUBLIC Python
Stack Overflow
Asked 7 years, 2 months ago Active 14 days ago Viewed 23k times
Tags
Users 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
FIND A JOB 11 data points from a histogram? I need actual values of Luminosities. I am
Jobs using Matplotlib in Python, and any online book, example, tutorial etc is not
helping.
Companies
2 l= [ss.gammainccinv(aa+1, u[i]) for i in a] #is the cdf function
TEAMS What’s this?
plt.hist(l, 50, histtype= 'step', align='mid') #is the histogram
Free 30 Day Trial 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!
python matplotlib histogram
Share Edit Follow asked Nov 21 '13 at 18:19
user3014593
131 1 1 5
add a comment
2 Answers Active Oldest Votes
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.
15
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.3981
0.49731133, 0.59645641, 0.69560149, 0.79474657, 0.89389
0.99303674]),
<a list of 10 Patch objects>)
So, to save them, do:
counts, bins, bars = plt.hist(x)
Share Edit Follow answered Nov 21 '13 at 19:30
askewchan
38.6k 13 103 127
yes, all I needed to do was "print l" instead of "print l[i]". That only gave the
first number from the array. Or a number. Learning from the silly mistakes.
Thank you! :) – user3014593 Nov 27 '13 at 16:04
add a comment
You can also retrieve info from an already plotted histogram. For example:
2 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)
Share Edit Follow answered Jan 15 at 16:25
Sinan Gok
131 4
add a comment
Your Answer
Links Images Styling/Headers Lists Blockquotes Code HTML Tables
Advanced help
Post Your Answer
Not the answer you're looking for? Browse other questions tagged python
matplotlib histogram or ask your own question.
The Overflow Blog
The Loop: Our Community & Public
Platform strategy & roadmap for Q1 2021
Podcast 308: What are the young
developers into? Everyone’s getting
AWS…
Featured on Meta
Opt-in alpha test for a new Stacks editor
Visual design changes to the review
queues
2020: a year in moderation
Hot Meta Posts
50 A way to reduce the number of questions
with only images?
12 Roll back latest edit of
https://stackoverflow.com/q/7122609?
36 Let's gift wrap our (good) machine
learning theory questions for Cross
Validated
Love this site?
Get the weekly newsletter! In it, you'll get:
The week's top questions and answers
Important community announcements
Questions that need answers
Sign up for the digest
see an example newsletter
Linked
0 Is it possible to manipulate the data in a
matplotlib histogram using Get and Set?
0 extract data points from a histogram in
matplotlib
Related
5372 How do I merge two dictionaries in a single
expression in Python (taking union of
dictionaries)?
5151 How to execute a program or call a system
command from Python?
6025 What are metaclasses in Python?
6456 Does Python have a ternary conditional
operator?
3136 How to get the current time in Python
2299 How do I get a substring of a string in
Python?
2438 Manually raising (throwing) an exception in
Python
3597 Does Python have a string 'contains'
substring method?
Hot Network Questions
As there is no unsigned int in SQL Server doesn't
an Identity Seed of -2,147,483,648 make more
sense for large tables?
That puts us almost at the same age, actually.
(Meaning in context)
Narrow or comfort saddle?
Carnival sideshow with a dinosaur
How hot is too hot for a transformer?
more hot questions
Question feed
STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Advertising Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Terms of Service
Disable Responsiveness Contact Us
Blog Facebook Twitter LinkedIn Instagram
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.1.29.38441