8
8
def calc_bins (n , min_val , max_val , h = None ):
9
9
"calculate number of bins for the histogram"
10
10
if not h :
11
- h = max (10 , math .log (n + 1 , 2 ))
11
+ h = max (10 , math .log (n + 1 , 2 ))
12
12
bin_width = (max_val - min_val ) / h
13
13
for b in drange (min_val , max_val , bin_width ):
14
14
yield b
@@ -44,8 +44,8 @@ def run_demo():
44
44
print "hist -f ./data/exp.txt -p ."
45
45
plot_hist ('./data/exp.txt' , pch = '.' )
46
46
print "*" * 80
47
- #chagning the size of the plot
48
- print "chagning the size of the plot"
47
+ #changing the size of the plot
48
+ print "changing the size of the plot"
49
49
print "plot_hist('./data/exp.txt', height=35.0, bincount=40)"
50
50
print "hist -f ./data/exp.txt -s 35.0 -b 40"
51
51
plot_hist ('./data/exp.txt' , height = 35.0 , bincount = 40 )
@@ -62,20 +62,20 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
62
62
xlab -- boolen value for whether or not to display x-axis labels
63
63
showSummary -- boolean value for whether or not to display a summary
64
64
"""
65
-
65
+
66
66
if pch is None :
67
67
pch = "o"
68
-
68
+
69
69
colour = get_colour (colour )
70
70
71
71
min_val , max_val = None , None
72
72
n , mean = 0. , 0.
73
73
for number in read_numbers (f ):
74
74
n += 1
75
75
76
- if not min_val or number < min_val :
76
+ if ( min_val is None ) or ( number < min_val ) :
77
77
min_val = number
78
- if not max_val or number > max_val :
78
+ if ( max_val is None ) or ( number > max_val ) :
79
79
max_val = number
80
80
mean += number
81
81
mean /= n
@@ -91,14 +91,14 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
91
91
break
92
92
93
93
min_y , max_y = min (hist .values ()), max (hist .values ())
94
-
94
+
95
95
ys = list (drange (min_y , max_y , (max_y - min_y )/ height ))
96
96
ys .reverse ()
97
-
97
+
98
98
nlen = max (len (str (min_y )), len (str (max_y ))) + 1
99
-
99
+
100
100
if title :
101
- print box_text (title , len (hist )* 2 , nlen )
101
+ print box_text (title , max ( len (hist )* 2 , len ( title )) , nlen )
102
102
print
103
103
used_labs = set ()
104
104
for y in ys :
@@ -123,18 +123,21 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
123
123
124
124
125
125
if xlab :
126
- for i in range (0 , nlen ):
126
+ xlen = len (str (float ((max_y )/ height ) + max_y ))
127
+ for i in range (0 , xlen ):
127
128
printcolor (" " * (nlen + 1 ), True , colour )
128
129
for x in range (0 , len (hist )):
129
130
num = str (bins [x ])
130
131
if x % 2 == 0 :
131
132
print " " ,
132
133
elif i < len (num ):
133
134
print num [i ],
135
+ else :
136
+ print " " ,
134
137
print
135
138
center = max (map (len , map (str , [n , min_val , mean , max_val ])))
136
139
center += 15
137
-
140
+
138
141
if showSummary :
139
142
print
140
143
print "-" * (2 + center )
0 commit comments