Question

For a Series:series

0 111.8040373

2 140.10805914

4 117.64930612

5 111.85077526

6 137.22535711

7 138.59732811

...

123 103.63270617

Could you provide me suggestion to plot the histogram of the series (e.g. pandas)? I also want to use a bin width (e.g. 5) in the range [0, 150]. I found a similar question but tried it and couldn't get it to work.

Was it helpful?

Solution

This is the script i personally use to make histograms.

import numpy as np
import pylab as P

def binner(LB, UB, step=1):
    N = int((UB-LB+1)/float(step) + 1)
    return [LB + step * (i - 1/float(2)) for i in xrange(N)]

def f_hist(x, bins=None, param=20, h_type='bar', barwidth=0.8,filename=None):
    P.figure()
    if bins is None:
        if hasattr(param, "__iter__"):
            if len(param) == 2:
                LB, UB = param
                step = 1
            elif len(param) == 3:    
                LB, UB, step = param
            else:
                raise Exception
            bins = binner(LB, UB, step)
            n, bins, patches = P.hist(x,bins,histtype=h_type,rwidth=barwidth)
        else:
            n, bins, patches = P.hist(x,param,histtype=h_type,rwidth=barwidth)
    else:
        n, bins, patches = P.hist(x, bins, histtype=h_type, rwidth=barwidth)    
    if filename is None:
        P.show()
    else:
        P.savefig(filename)
        print "Histogram saved as {}".format(filename)
        return filename

if __name__ == '__main__':
    mu, sigma = 200, 25
    LB, UB, step = 100, 300, 10
    x = mu + sigma*P.randn(10000)
    f_hist(x, param = (LB, UB, step))
    f_hist(x)

You can see sample case with a normal distribution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top