55from utils .helpers import *
66from utils .commandhelp import hist
77
8- def calc_bins (n , min_val , max_val , h = None ):
8+ def calc_bins (n , min_val , max_val , h = None , binwidth = None ):
99 "calculate number of bins for the histogram"
1010 if not h :
1111 h = max (10 , math .log (n + 1 , 2 ))
12- bin_width = (max_val - min_val ) / h
13- for b in drange (min_val , max_val , bin_width ):
12+ if binwidth == 0 :
13+ binwidth = 0.1
14+ if binwidth is None :
15+ binwidth = (max_val - min_val ) / h
16+ for b in drange (min_val , max_val , step = binwidth , include_stop = True ):
1417 yield b
1518
1619def read_numbers (numbers ):
@@ -50,12 +53,13 @@ def run_demo():
5053 print "hist -f ./data/exp.txt -s 35.0 -b 40"
5154 plot_hist ('./data/exp.txt' , height = 35.0 , bincount = 40 )
5255
53- def plot_hist (f , height = 20.0 , bincount = None , pch = "o" , colour = "white" , title = "" , xlab = None , showSummary = False ):
56+ def plot_hist (f , height = 20.0 , bincount = None , binwidth = None , pch = "o" , colour = "white" , title = "" , xlab = None , showSummary = False ):
5457 """make a histogram
5558
5659 Keyword arguments:
5760 height -- the height of the histogram in # of lines
5861 bincount -- number of bins in the histogram
62+ binwidth -- width of bins in the histogram
5963 pch -- shape of the bars in the plot
6064 colour -- colour of the bars in the terminal
6165 title -- title at the top of the plot
@@ -80,7 +84,7 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
8084 mean += number
8185 mean /= n
8286
83- bins = list (calc_bins (n , min_val , max_val , bincount ))
87+ bins = list (calc_bins (n , min_val , max_val , bincount , binwidth ))
8488 hist = {}
8589 for i in range (len (bins )):
8690 hist [i ] = 0
@@ -161,6 +165,8 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
161165 default = "" , dest = 't' )
162166 parser .add_option ('-b' , '--bins' , help = 'number of bins in the histogram' ,
163167 type = 'int' , default = None , dest = 'b' )
168+ parser .add_option ('-w' , '--binwidth' , help = 'width of bins in the histogram' ,
169+ type = 'float' , default = None , dest = 'binwidth' )
164170 parser .add_option ('-s' , '--height' , help = 'height of the histogram (in lines)' ,
165171 type = 'int' , default = 20. , dest = 'h' )
166172 parser .add_option ('-p' , '--pch' , help = 'shape of each bar' , default = 'o' , dest = 'p' )
@@ -181,7 +187,7 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
181187 if opts .demo :
182188 run_demo ()
183189 elif opts .f :
184- plot_hist (opts .f , opts .h , opts .b , opts .p , opts .colour , opts .t , opts .x , opts .showSummary )
190+ plot_hist (opts .f , opts .h , opts .b , opts .binwidth , opts . p , opts .colour , opts .t , opts .x , opts .showSummary )
185191 else :
186192 print "nothing to plot!"
187193
0 commit comments