Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas : histogram with fixed width [closed]

I have data which I want to do an histogram, but I want the histogram to start from a given value and the width of a bar to be fixed. For example, for the serie [1, 3, 5, 10, 12, 20, 21, 25], I want, instead of

>>> p.Series([1, 3, 5, 10, 12, 20, 21, 25]).hist(bins=3).figure

#  |       |
#  |   |   |
#  |   |   |
#  0   8.5 17

Current histogram

I want the bars to have a width of 10 :

|       |
|   |   |
|   |   |
0   10  20

How can I do that ?

EDIT : I eventually get what I wanted good hist

like image 528
MatthieuBizien Avatar asked Jul 08 '13 09:07

MatthieuBizien


People also ask

How can you plot histogram for a particular column Column_name of a Dataframe?

In order to plot a histogram using pandas, chain the . hist() function to the dataframe. This will return the histogram for each numeric column in the pandas dataframe.

What is PD cut in Pandas?

Pandas cut() function is used to separate the array elements into different bins . The cut function is mainly used to perform statistical analysis on scalar data.

How do you set a range in a histogram in Python?

To create a histogram the first step is to create bin of the ranges, then distribute the whole range of the values into a series of intervals, and count the values which fall into each of the intervals. Bins are clearly identified as consecutive, non-overlapping intervals of variables.


1 Answers

I think

p.Series([1, 3, 5, 10, 12, 20, 21, 25]).hist(bins=[0, 10, 20, 30]).figure

will do what you want. Alternately you can do

p.Series([1, 3, 5, 10, 12, 20, 21, 25]).hist(bins=3, range=(0,30)).figure

See documentation for hist and the documentation for np.histogram.

I suspect you are also running into some issues because it is labeling the center of the bins, not the edges.

like image 187
tacaswell Avatar answered Oct 13 '22 01:10

tacaswell