Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions bashplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from utils.helpers import *
from utils.commandhelp import hist

def calc_bins(n, min_val, max_val, h=None):
def calc_bins(n, min_val, max_val, h=None, binwidth=None):
"calculate number of bins for the histogram"
if not h:
h = max(10, math.log(n + 1, 2))
bin_width = (max_val - min_val) / h
for b in drange(min_val, max_val, bin_width):
if binwidth == 0:
binwidth = 0.1
if binwidth is None:
binwidth = (max_val - min_val) / h
for b in drange(min_val, max_val, step=binwidth, include_stop=True):
yield b

def read_numbers(numbers):
Expand Down Expand Up @@ -50,12 +53,13 @@ def run_demo():
print "hist -f ./data/exp.txt -s 35.0 -b 40"
plot_hist('./data/exp.txt', height=35.0, bincount=40)

def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="", xlab=None, showSummary=False):
def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="white", title="", xlab=None, showSummary=False):
"""make a histogram

Keyword arguments:
height -- the height of the histogram in # of lines
bincount -- number of bins in the histogram
binwidth -- width of bins in the histogram
pch -- shape of the bars in the plot
colour -- colour of the bars in the terminal
title -- title at the top of the plot
Expand All @@ -80,7 +84,7 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
mean += number
mean /= n

bins = list(calc_bins(n, min_val, max_val, bincount))
bins = list(calc_bins(n, min_val, max_val, bincount, binwidth))
hist = {}
for i in range(len(bins)):
hist[i] = 0
Expand Down Expand Up @@ -161,6 +165,8 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
default="", dest='t')
parser.add_option('-b', '--bins', help='number of bins in the histogram',
type='int', default=None, dest='b')
parser.add_option('-w', '--binwidth', help='width of bins in the histogram',
type='float', default=None, dest='binwidth')
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
type='int', default=20., dest='h')
parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p')
Expand All @@ -181,7 +187,7 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
if opts.demo:
run_demo()
elif opts.f:
plot_hist(opts.f, opts.h, opts.b, opts.p, opts.colour, opts.t, opts.x, opts.showSummary)
plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary)
else:
print "nothing to plot!"

15 changes: 10 additions & 5 deletions bashplotlib/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@ def printcolor(txt, sameline=False, color=get_colour("white")):
else:
print color + txt + bcolours["ENDC"]

def drange(start, stop, step=1.0):
"generate between 2 numbers w/ optional step"
def drange(start, stop, step=1.0, include_stop=False):
"generate between 2 numbers w/ optional step, optionally include upper bound"
if step==0:
step = 0.01
r = start
while r < stop:
yield r
r += step
if include_stop:
while r <= stop:
yield r
r += step
else:
while r < stop:
yield r
r += step

def box_text(text, width, offset=0):
box = " "*offset + "-"*(width+2) + "\n"
Expand Down