2

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.

Community
  • 1
  • 1
jiadong
  • 318
  • 5
  • 16
  • I don't know what you mean with a histogram of two variables. What are you expected it to look like? – Chris Hagmann Apr 26 '14 at 14:58
  • @cdhagmann Sorry for the blurred expression.For example, I will count how many number of the data are located in the interval (0,5),(5,10),...,(145,150), repectively. – jiadong Apr 26 '14 at 15:07
  • But are we looking at `0, 2, 4, ... 123` or at the `111.8040373...` series? or are you hoping for something that does both? – Chris Hagmann Apr 26 '14 at 15:10
  • We consider only the 2nd column, the first one includes the lables. – jiadong Apr 26 '14 at 15:13
  • if it's a pandas series, can't you just do `data.hist( bins=30, range=[0,150] )`, where `data` is your series? am I understanding your question properly? – Higgs Apr 26 '14 at 19:23
  • 1
    @ycy Thanks! Yes, you are right! hist may work. Follow you suggest, I have tried ``hist(series,bins=30, range=[0,150]).figure`` it works, but, when I tried ``series.hist( bins=30, range=[0,150] )`` the figure does not come out. – jiadong Apr 26 '14 at 19:46

1 Answers1

0

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.

Chris Hagmann
  • 1,086
  • 8
  • 14