99def calc_bins (n , min_val , max_val , h = None ):
1010 "calculate number of bins for the histogram"
1111 if not h :
12- h = math .log (n + 1 , 2 )
12+ h = max ( 10 , math .log (n + 1 , 2 ) )
1313 bin_width = (max_val - min_val ) / h
1414 for b in drange (min_val , max_val , bin_width ):
1515 yield b
@@ -22,7 +22,7 @@ def read_numbers(numbers):
2222 for n in open (numbers ):
2323 yield float (n .strip ())
2424
25- def plot_hist (f , height = 20 , bincount = None , pch = "o" , colour = "white" , title = "" ):
25+ def plot_hist (f , height = 20.0 , bincount = None , pch = "o" , colour = "white" , title = "" ):
2626 "plot a histogram given a file of numbers"
2727 #first apss
2828 if pch is None :
@@ -43,27 +43,30 @@ def plot_hist(f, height=20, bincount=None, pch="o", colour="white", title=""):
4343 mean /= n
4444
4545 bins = list (calc_bins (n , min_val , max_val , bincount ))
46- hist = Counter ()
46+ hist = { i : 0 for i in range ( len ( bins ))}
4747 for number in read_numbers (f ):
4848 for i , b in enumerate (bins ):
4949 if number < b :
5050 hist [i - 1 ] += 1
5151 # print "breaking"
5252 break
5353
54-
55-
5654 min_y , max_y = min (hist .values ()), max (hist .values ())
57-
55+
5856 ys = list (drange (min_y , max_y , (max_y - min_y )/ height ))
5957 ys .reverse ()
60-
58+
6159 nlen = max (len (str (min_y )), len (str (max_y ))) + 1
62-
60+ nlen = 20
6361 print title .center (len (hist ) + nlen + 1 )
6462 print
63+ used_labs = set ()
6564 for y in ys :
66- ylab = str (y )
65+ ylab = str (int (y ))
66+ if ylab in used_labs :
67+ ylab = ""
68+ else :
69+ used_labs .add (ylab )
6770 ylab += " " * (nlen - len (ylab )) + "|"
6871
6972 print ylab ,
@@ -115,7 +118,7 @@ def plot_hist(f, height=20, bincount=None, pch="o", colour="white", title=""):
115118 parser .add_option ('-b' , '--bins' , help = 'number of bins in the histogram' ,
116119 type = 'int' , default = None , dest = 'b' )
117120 parser .add_option ('-s' , '--height' , help = 'height of the histogram (in lines)' ,
118- type = 'int' , default = 20 , dest = 'h' )
121+ type = 'int' , default = 20. , dest = 'h' )
119122 parser .add_option ('-p' , '--pch' , help = 'shape of each bar' , default = 'o' , dest = 'p' )
120123 parser .add_option ('-c' , '--colour' , help = 'colour of the plot (%s)' % ", " .join (bcolours .keys ()),
121124 default = 'white' , dest = 'colour' )
0 commit comments