Histogram in pandas plots the count of each bin, rather than the normalized fraction. In R, this is an option in the histogram. Is it possible in Pandas? If not, any recommendations for an easy workaround?
Asked
Active
Viewed 1.3k times
2 Answers
6
For me this gives the desired results.
df = pd.DataFrame(np.random.randn(5000))
df.hist(normed = True)
The 'density' option works in numpy's histogram function but not on pandas's hist function.

BKay
- 1,397
- 1
- 15
- 26
-
Normed gives me values of like 14 though. Is it percentages? – wolfsatthedoor Aug 29 '14 at 23:29
-
Would i need df.np.hist then? – wolfsatthedoor Aug 29 '14 at 23:30
-
You mean the y-axis? What do you get when you use df.hist(normed = True) and df.hist(normed = False)? Both peak at zero but the former takes a maximum of about .4 and the other a maximum of about 1400. – BKay Aug 29 '14 at 23:35
-
oddly enough, when I do normed, I get a max of 8 on my Yaxis and when I do unnormed, I get a max of 3600. Normed should put the Y axis between 0 and 1, unless it's not doing what I think it is... – wolfsatthedoor Aug 31 '14 at 02:11
-
Weird. That's not what I get. Do you get that 8 result on my sample code, your data frame, or both? – BKay Aug 31 '14 at 13:09
-
My data frame, I looked through other stack questions and noticed other people got this, too. No good explanations about what "normed" is doing. – wolfsatthedoor Aug 31 '14 at 20:15
3
You can pass density parameter to hist, like this
df.hist(..., density=True)
Here, density is passed as kwds to np.hist.
Reference: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.hist.html http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html

K.Chen
- 1,166
- 1
- 11
- 18
-
How can I make it recognize NUMPY hist when I have both pandas and Numpy loaded? – wolfsatthedoor Aug 31 '14 at 02:12
-
Here, density is passed as kwds to np.hist. So it is automatically recognized. (it is written on pandas documentation.) – K.Chen Aug 31 '14 at 04:21