Skip to content
Prev Previous commit
Next Next commit
re-write scatter plot, update docs
  • Loading branch information
TrungNT committed Jan 17, 2016
commit 9dde89934d0fc5405e684d04cce7f7a3c184454d
50 changes: 36 additions & 14 deletions bashplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,42 @@ def run_demo():


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

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
xlab -- boolen value for whether or not to display x-axis labels
showSummary -- boolean value for whether or not to display a summary
regular -- boolean value for whether or not to start y-labels at 0
"""
''' Plot histogram.
1801| oo
1681| oo
1561| oooo
961| oooo
841| oooo
721| ooooo
601| oooooo
241| oooooo
121| oooooooo
1| oooooooooooooo
--------------
Parameters
----------
f : list(number), numpy.ndarray, str(filepath)
input array
height : float
the height of the histogram in # of lines
bincount : int
number of bins in the histogram
binwidth : int
width of bins in the histogram
pch : str
shape of the bars in the plot, e.g 'o'
colour : str
white,aqua,pink,blue,yellow,green,red,grey,black,default,ENDC
title : str
title at the top of the plot, None = no title
xlab : boolean
whether or not to display x-axis labels
showSummary : boolean
whether or not to display a summary
regular : boolean
whether or not to start y-labels at 0

'''
if pch is None:
pch = "o"

Expand Down
77 changes: 54 additions & 23 deletions bashplotlib/scatterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,58 @@ def get_scale(series, is_y=False, steps=20):
return scaled_series


def plot_scatter(f, xs, ys, size, pch, colour, title):
def plot_scatter(xs, ys, size=None, pch='o', colour='red', title=None, print_func=print):
''' Scatter plot.
----------------------
| * |
| * |
| * |
| * |
| * |
| * |
| * |
| * |
-----------------------
Parameters
----------
xs : list, numpy.ndarray
list of x series
ys : list, numpy.ndarray
list of y series
size : int
width of plot
pch : str
any character to represent a points
colour : str, list(str)
white,aqua,pink,blue,yellow,green,red,grey,black,default,ENDC
title : str
title for the plot, None = not show
print_func : callable
function for print out a string

'''
plotted = set()
cs = colour

if title:
print_func(box_text(title, 2 * len(get_scale(xs, False, size)) + 1))

print_func("-" * (2 * len(get_scale(xs, False, size)) + 2))
for y in get_scale(ys, True, size):
print_func("|", end=' ')
for x in get_scale(xs, False, size):
point = " "
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
if xp <= x and yp >= y and (xp, yp) not in plotted:
point = pch
plotted.add((xp, yp))
if isinstance(cs, list):
colour = cs[i]
printcolour(point, True, colour)
print_func(" |")
print_func("-" * (2 * len(get_scale(xs, False, size)) + 2))

def _plot_scatter(f, xs, ys, size, pch, colour, title):
"""
Form a complex number.

Expand Down Expand Up @@ -60,27 +111,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
cs = [str(row).strip() for row in open(colour)]
else:
cs = colour

plotted = set()

if title:
print(box_text(title, 2 * len(get_scale(xs, False, size)) + 1))

print("-" * (2 * len(get_scale(xs, False, size)) + 2))
for y in get_scale(ys, True, size):
print("|", end=' ')
for x in get_scale(xs, False, size):
point = " "
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
if xp <= x and yp >= y and (xp, yp) not in plotted:
point = pch
plotted.add((xp, yp))
if isinstance(cs, list):
colour = cs[i]
printcolour(point, True, colour)
print(" |")
print("-" * (2 * len(get_scale(xs, False, size)) + 2))

plot_scatter(xs, ys, size=size, pch=pch, colour=cs, title=title)

def main():

Expand All @@ -101,7 +132,7 @@ def main():
opts.f = sys.stdin.readlines()

if opts.f or (opts.x and opts.y):
plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t)
_plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t)
else:
print("nothing to plot!")

Expand Down