Skip to content

Commit 6ff11e6

Browse files
author
Mackenzie
committed
y-axis changes
add 'regular' option to start y-values at 0 instead of min y-val change height defaults now graphs showing a range of y-values smaller than 20 are shown with a shorter height related fix: modify drange calculation slightly to prevent undesired side effects of float arithmetic
1 parent 6871f05 commit 6ff11e6

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

bashplotlib/histogram.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def run_demo():
5353
print "hist -f ./data/exp.txt -s 35.0 -b 40"
5454
plot_hist('./data/exp.txt', height=35.0, bincount=40)
5555

56-
def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="white", title="", xlab=None, showSummary=False):
56+
def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="white", title="", xlab=None, showSummary=False, regular=False):
5757
"""make a histogram
5858
5959
Keyword arguments:
@@ -65,6 +65,7 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi
6565
title -- title at the top of the plot
6666
xlab -- boolen value for whether or not to display x-axis labels
6767
showSummary -- boolean value for whether or not to display a summary
68+
regular -- boolean value for whether or not to start y-labels at 0
6869
"""
6970

7071
if pch is None:
@@ -98,7 +99,17 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi
9899

99100
min_y, max_y = min(hist.values()), max(hist.values())
100101

101-
ys = list(drange(max(min_y, 1), max_y + 1, (max_y-min_y)/height))
102+
start = max(min_y, 1)
103+
stop = max_y + 1
104+
if regular:
105+
start = 1
106+
107+
if height is None:
108+
height = stop - start
109+
if height > 20:
110+
height = 20
111+
112+
ys = list(drange(start, stop, float(stop - start)/height))
102113
ys.reverse()
103114

104115
nlen = max(len(str(min_y)), len(str(max_y))) + 1
@@ -170,13 +181,14 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi
170181
parser.add_option('-w', '--binwidth', help='width of bins in the histogram',
171182
type='float', default=None, dest='binwidth')
172183
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
173-
type='int', default=20., dest='h')
184+
type='int', default=None, dest='h')
174185
parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p')
175186
parser.add_option('-x', '--xlab', help='label bins on x-axis', default=None, action="store_true", dest='x')
176187
parser.add_option('-c', '--colour', help='colour of the plot (%s)' % ", ".join([c for c in bcolours.keys() if c != 'ENDC']),
177188
default='white', dest='colour')
178189
parser.add_option('-d', '--demo', help='run demos', action='store_true', dest='demo')
179190
parser.add_option('-n', '--nosummary', help='hide summary', action='store_false', dest='showSummary', default=True)
191+
parser.add_option('-r', '--regular', help='use regular y-scale (0 - maximum y value), instead of truncated y-scale (minimum y-value - maximum y-value)', default=False, action="store_true", dest='regular')
180192

181193
(opts, args) = parser.parse_args()
182194

@@ -189,7 +201,7 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="whi
189201
if opts.demo:
190202
run_demo()
191203
elif opts.f:
192-
plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary)
204+
plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary, opts.regular)
193205
else:
194206
print "nothing to plot!"
195207

bashplotlib/utils/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ def drange(start, stop, step=1.0, include_stop=False):
3636
while r <= stop:
3737
yield r
3838
r += step
39+
r = round(r, 10)
3940
else:
4041
while r < stop:
4142
yield r
4243
r += step
44+
r = round(r, 10)
4345

4446
def box_text(text, width, offset=0):
4547
box = " "*offset + "-"*(width+2) + "\n"
4648
box += " "*offset + "|"+ text.center(width) + "|" + "\n"
4749
box += " "*offset + "-"*(width+2)
4850
return box
4951

50-

0 commit comments

Comments
 (0)