Search code examples
pythonpandasmatplotlibbinning

Plot bin sizes in Python


I've cut my data into several bins and would like to plot the size/frequency of each bin in a histogram. With the y-axis being the bin frequency and the x labels being the bin ranges.

Currently I have:

out = pd.cut(data.hour, bins = filter_values, include_lowest = True)
counts = pd.value_counts(out)
print(counts)`

Which outputs:

[0, 5]      1000
(19, 23]       0
(15, 19]       0
(11, 15]       0
(8, 11]        0
(5, 8]         0
Name: hour, dtype: int64

How would I go about this?


Solution

  • Demo (step by step):

    In [51]: filter_values = [0, 5, 11, 15, 19, 23]
    

    generating sample DF:

    In [52]: df = pd.DataFrame({'hour':np.random.randint(0, 23, 20)})
    
    In [53]: df
    Out[53]:
        hour
    0      5
    1     18
    2     19
    3      5
    4     16
    5     18
    6     18
    7     19
    8      0
    9      8
    10    14
    11    20
    12     9
    13    22
    14     8
    15     0
    16     0
    17     4
    18    13
    19    18
    

    building bins

    In [54]: out = pd.cut(data.hour, bins = filter_values, include_lowest = True)
    
    In [55]: out
    Out[55]:
    0       [0, 5]
    1     (11, 15]
    2     (15, 19]
    3     (11, 15]
    4      (5, 11]
    5       [0, 5]
    6       [0, 5]
    7       [0, 5]
    8       [0, 5]
    9      (5, 11]
    10      [0, 5]
    11      [0, 5]
    12    (11, 15]
    13    (15, 19]
    14     (5, 11]
    15    (15, 19]
    16     (5, 11]
    17    (19, 23]
    18     (5, 11]
    19      [0, 5]
    Name: hour, dtype: category
    Categories (5, object): [[0, 5] < (5, 11] < (11, 15] < (15, 19] < (19, 23]]
    

    counts:

    In [56]: counts = out.value_counts(sort=False)
    
    In [57]: counts
    Out[57]:
    [0, 5]      8
    (5, 11]     5
    (11, 15]    3
    (15, 19]    3
    (19, 23]    1
    Name: hour, dtype: int64
    

    plot

    In [58]: counts.plot.bar(rot=False)
    Out[58]: <matplotlib.axes._subplots.AxesSubplot at 0xa427b00>
    

    enter image description here