Skip to content

Commit 6bf4681

Browse files
committed
Merge pull request #10 from mc818/cleanup
cleanup
2 parents 4307823 + 4cb91a0 commit 6bf4681

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

bashplotlib/histogram.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
def calc_bins(n, min_val, max_val, h=None):
99
"calculate number of bins for the histogram"
1010
if not h:
11-
h = max(10, math.log(n + 1, 2))
11+
h = max(10, math.log(n + 1, 2))
1212
bin_width = (max_val - min_val) / h
1313
for b in drange(min_val, max_val, bin_width):
1414
yield b
@@ -44,8 +44,8 @@ def run_demo():
4444
print "hist -f ./data/exp.txt -p ."
4545
plot_hist('./data/exp.txt', pch='.')
4646
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"
4949
print "plot_hist('./data/exp.txt', height=35.0, bincount=40)"
5050
print "hist -f ./data/exp.txt -s 35.0 -b 40"
5151
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="",
6262
xlab -- boolen value for whether or not to display x-axis labels
6363
showSummary -- boolean value for whether or not to display a summary
6464
"""
65-
65+
6666
if pch is None:
6767
pch = "o"
68-
68+
6969
colour = get_colour(colour)
7070

7171
min_val, max_val = None, None
7272
n, mean = 0., 0.
7373
for number in read_numbers(f):
7474
n += 1
7575

76-
if not min_val or number < min_val:
76+
if (min_val is None) or (number < min_val):
7777
min_val = number
78-
if not max_val or number > max_val:
78+
if (max_val is None) or (number > max_val):
7979
max_val = number
8080
mean += number
8181
mean /= n
@@ -91,14 +91,14 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
9191
break
9292

9393
min_y, max_y = min(hist.values()), max(hist.values())
94-
94+
9595
ys = list(drange(min_y, max_y, (max_y-min_y)/height))
9696
ys.reverse()
97-
97+
9898
nlen = max(len(str(min_y)), len(str(max_y))) + 1
99-
99+
100100
if title:
101-
print box_text(title, len(hist)*2, nlen)
101+
print box_text(title, max(len(hist)*2, len(title)), nlen)
102102
print
103103
used_labs = set()
104104
for y in ys:
@@ -123,18 +123,21 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
123123

124124

125125
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):
127128
printcolor(" "*(nlen+1), True, colour)
128129
for x in range(0, len(hist)):
129130
num = str(bins[x])
130131
if x%2==0:
131132
print " ",
132133
elif i < len(num):
133134
print num[i],
135+
else:
136+
print " ",
134137
print
135138
center = max(map(len, map(str, [n, min_val, mean, max_val])))
136139
center += 15
137-
140+
138141
if showSummary:
139142
print
140143
print "-"*(2 + center)

0 commit comments

Comments
 (0)