GeezerVison is showing github.com — Merge pull request #12 from mc818/binwidth · NeelShah18/bashplotlib@6871f05
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bashplotlib/histogram.py +12-6Lines changed: 12 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | from utils.helpers import * | 6 | 6 | from utils.commandhelp import hist | 7 | 7 |
| 8 | | -def calc_bins(n, min_val, max_val, h=None): | | 8 | +def calc_bins(n, min_val, max_val, h=None, binwidth=None): | 9 | 9 | "calculate number of bins for the histogram" | 10 | 10 | if not h: | 11 | 11 | h = max(10, math.log(n + 1, 2)) | 12 | | - bin_width = (max_val - min_val) / h | 13 | | - for b in drange(min_val, max_val, bin_width): | | 12 | + if binwidth == 0: | | 13 | + binwidth = 0.1 | | 14 | + if binwidth is None: | | 15 | + binwidth = (max_val - min_val) / h | | 16 | + for b in drange(min_val, max_val, step=binwidth, include_stop=True): | 14 | 17 | yield b | 15 | 18 |
| 16 | 19 | def read_numbers(numbers): | @@ -50,12 +53,13 @@ def run_demo(): | 50 | 53 | print "hist -f ./data/exp.txt -s 35.0 -b 40" | 51 | 54 | plot_hist('./data/exp.txt', height=35.0, bincount=40) | 52 | 55 |
| 53 | | -def plot_hist(f, height=20.0, bincount=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): | 54 | 57 | """make a histogram | 55 | 58 |
| 56 | 59 | Keyword arguments: | 57 | 60 | height -- the height of the histogram in # of lines | 58 | 61 | bincount -- number of bins in the histogram | | 62 | + binwidth -- width of bins in the histogram | 59 | 63 | pch -- shape of the bars in the plot | 60 | 64 | colour -- colour of the bars in the terminal | 61 | 65 | 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="", | 80 | 84 | mean += number | 81 | 85 | mean /= n | 82 | 86 |
| 83 | | - bins = list(calc_bins(n, min_val, max_val, bincount)) | | 87 | + bins = list(calc_bins(n, min_val, max_val, bincount, binwidth)) | 84 | 88 | hist = {} | 85 | 89 | for i in range(len(bins)): | 86 | 90 | hist[i] = 0 | @@ -163,6 +167,8 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="", | 163 | 167 | default="", dest='t') | 164 | 168 | parser.add_option('-b', '--bins', help='number of bins in the histogram', | 165 | 169 | 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') | 166 | 172 | parser.add_option('-s', '--height', help='height of the histogram (in lines)', | 167 | 173 | type='int', default=20., dest='h') | 168 | 174 | 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="", | 183 | 189 | if opts.demo: | 184 | 190 | run_demo() | 185 | 191 | elif opts.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) | 187 | 193 | else: | 188 | 194 | print "nothing to plot!" | 189 | 195 |
|
Collapse file Copy file name to clipboardExpand all lines: bashplotlib/utils/helpers.py +10-5Lines changed: 10 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -27,14 +27,19 @@ def printcolor(txt, sameline=False, color=get_colour("white")): | 27 | 27 | else: | 28 | 28 | print color + txt + bcolours["ENDC"] | 29 | 29 |
| 30 | | -def drange(start, stop, step=1.0): | 31 | | - "generate between 2 numbers w/ optional step" | | 30 | +def drange(start, stop, step=1.0, include_stop=False): | | 31 | + "generate between 2 numbers w/ optional step, optionally include upper bound" | 32 | 32 | if step==0: | 33 | 33 | step = 0.01 | 34 | 34 | r = start | 35 | | - while r < stop: | 36 | | - yield r | 37 | | - r += step | | 35 | + if include_stop: | | 36 | + while r <= stop: | | 37 | + yield r | | 38 | + r += step | | 39 | + else: | | 40 | + while r < stop: | | 41 | + yield r | | 42 | + r += step | 38 | 43 |
| 39 | 44 | def box_text(text, width, offset=0): | 40 | 45 | box = " "*offset + "-"*(width+2) + "\n" | |
|