Skip to content

Commit 6995522

Browse files
author
Michael Wang
committed
bug fix for histogram 1). default scatter colour to None to remove strange characters in pipe output; 2). fix broken pipe during piping
1 parent 767c14f commit 6995522

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

bashplotlib/histogram.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from .utils.helpers import *
1717
from .utils.commandhelp import hist
1818

19+
from signal import signal, SIGPIPE, SIG_DFL
20+
signal(SIGPIPE,SIG_DFL)
21+
1922

2023
def calc_bins(n, min_val, max_val, h=None, binwidth=None):
2124
"""
@@ -42,9 +45,8 @@ def read_numbers(numbers):
4245
for number in numbers:
4346
yield float(str(number).strip())
4447
else:
45-
with open(numbers) as fh:
46-
for number in fh:
47-
yield float(number.strip())
48+
for number in open(numbers):
49+
yield float(number.strip())
4850

4951

5052
def run_demo():
@@ -107,11 +109,10 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
107109
pch = "o"
108110

109111
if isinstance(f, str):
110-
with open(f) as fh:
111-
f = fh.readlines()
112+
f = open(f).readlines()
112113

113114
min_val, max_val = None, None
114-
n, mean, sd = 0.0, 0.0, 0.0
115+
n, mean = 0.0, 0.0
115116

116117
for number in read_numbers(f):
117118
n += 1
@@ -123,12 +124,6 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
123124

124125
mean /= n
125126

126-
for number in read_numbers(f):
127-
sd += (mean - number)**2
128-
129-
sd /= (n - 1)
130-
sd **= 0.5
131-
132127
bins = list(calc_bins(n, min_val, max_val, bincount, binwidth))
133128
hist = dict((i, 0) for i in range(len(bins)))
134129

@@ -184,12 +179,11 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
184179
print(" " * (nlen + 1) + "-" * len(xs))
185180

186181
if xlab:
187-
labels = abbreviate([str(b) for b in bins])
188-
xlen = len(labels[0])
182+
xlen = len(str(float((max_y) / height) + max_y))
189183
for i in range(0, xlen):
190184
printcolour(" " * (nlen + 1), True, colour)
191185
for x in range(0, len(hist)):
192-
num = labels[x]
186+
num = str(bins[x])
193187
if x % 2 != 0:
194188
pass
195189
elif i < len(num):
@@ -209,7 +203,6 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
209203
summary = "|" + ("observations: %d" % n).center(center) + "|\n"
210204
summary += "|" + ("min value: %f" % min_val).center(center) + "|\n"
211205
summary += "|" + ("mean : %f" % mean).center(center) + "|\n"
212-
summary += "|" + ("std dev : %f" % sd).center(center) + "|\n"
213206
summary += "|" + ("max value: %f" % max_val).center(center) + "|\n"
214207
summary += "-" * (2 + center)
215208
print(summary)
@@ -232,7 +225,7 @@ def main():
232225
parser.add_option('-x', '--xlab', help='label bins on x-axis',
233226
default=None, action="store_true", dest='x')
234227
parser.add_option('-c', '--colour', help='colour of the plot (%s)' %
235-
colour_help, default='default', dest='colour')
228+
colour_help, default=None, dest='colour')
236229
parser.add_option('-d', '--demo', help='run demos', action='store_true', dest='demo')
237230
parser.add_option('-n', '--nosummary', help='hide summary',
238231
action='store_false', dest='showSummary', default=True)

0 commit comments

Comments
 (0)