Skip to content

Commit 6871f05

Browse files
committed
Merge pull request glamp#12 from mc818/binwidth
add binwidth option
2 parents e15f071 + e0c0647 commit 6871f05

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

‎bashplotlib/histogram.py‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
fromutils.helpersimport*
66
fromutils.commandhelpimporthist
77

8-
defcalc_bins(n, min_val, max_val, h=None):
8+
defcalc_bins(n, min_val, max_val, h=None, binwidth=None):
99
"calculate number of bins for the histogram"
1010
ifnoth:
1111
h=max(10, math.log(n+1, 2))
12-
bin_width= (max_val-min_val) /h
13-
forbindrange(min_val, max_val, bin_width):
12+
ifbinwidth==0:
13+
binwidth=0.1
14+
ifbinwidthisNone:
15+
binwidth= (max_val-min_val) /h
16+
forbindrange(min_val, max_val, step=binwidth, include_stop=True):
1417
yieldb
1518

1619
defread_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-
defplot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="", xlab=None, showSummary=False):
56+
defplot_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
foriinrange(len(bins)):
8690
hist[i] =0
@@ -163,6 +167,8 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
163167
default="", dest='t')
164168
parser.add_option('-b', '--bins', help='number of bins in the histogram',
165169
type='int', default=None, dest='b')
170+
parser.add_option('-w', '--binwidth', help='width of bins in the histogram',
171+
type='float', default=None, dest='binwidth')
166172
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
167173
type='int', default=20., dest='h')
168174
parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p')
@@ -183,7 +189,7 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
183189
ifopts.demo:
184190
run_demo()
185191
elifopts.f:
186-
plot_hist(opts.f, opts.h, opts.b, opts.p, opts.colour, opts.t, opts.x, opts.showSummary)
192+
plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary)
187193
else:
188194
print"nothing to plot!"
189195

‎bashplotlib/utils/helpers.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ def printcolor(txt, sameline=False, color=get_colour("white")):
2727
else:
2828
printcolor+txt+bcolours["ENDC"]
2929

30-
defdrange(start, stop, step=1.0):
31-
"generate between 2 numbers w/ optional step"
30+
defdrange(start, stop, step=1.0, include_stop=False):
31+
"generate between 2 numbers w/ optional step, optionally include upper bound"
3232
ifstep==0:
3333
step=0.01
3434
r=start
35-
whiler<stop:
36-
yieldr
37-
r+=step
35+
ifinclude_stop:
36+
whiler<=stop:
37+
yieldr
38+
r+=step
39+
else:
40+
whiler<stop:
41+
yieldr
42+
r+=step
3843

3944
defbox_text(text, width, offset=0):
4045
box=" "*offset+"-"*(width+2) +"\n"

0 commit comments

Comments
(0)