diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 69cab9d..8e3b313 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -9,6 +9,9 @@ import csv import sys import optparse +from email.policy import default +from pydoc import describe + from .utils.helpers import * from .utils.commandhelp import scatter @@ -28,28 +31,51 @@ def get_scale(series, is_y=False, steps=20): return scaled_series -def _plot_scatter(xs, ys, size, pch, colour, title, cs): +def _plot_scatter(xs, ys, size, pch, colour, title, title_align, cs, xtitle, ytitle): plotted = set() + width = len(get_scale(xs, False, size)) + xscale = get_scale(xs,False,size) if title: - print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))) + print(box_text(title, title_align, 2 * (width + 1))) + + if ytitle: + print("y: " + ytitle) - print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) - for y in get_scale(ys, True, size): + print("-" * (2 * (width + 2))) + for y in get_scale(ys, True, size): #y axis loop print("|", end=' ') - for x in get_scale(xs, False, size): + + for x in get_scale(xs, False, size): #x axis loop 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)) + plotted.add((xp, yp)) # add the point on the graph if cs: colour = cs[i] - printcolour(point + " ", True, colour) + #printcolour(point + " ", True, colour) + + if point == pch: + printcolour(point + " ", True, colour) + else: + if x == 0 and y == 0: + printcolour("o ", True, colour) + elif y == 0: + printcolour("- ", True, colour) + elif x == 0: + printcolour("| ", True, colour) + else: + printcolour(point + " ", True, colour) + + print(" |") - print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + print("-" * (2 * (width + 2))) + + if xtitle: + print(str("x: " + xtitle).rjust(width)) -def plot_scatter(f, xs, ys, size, pch, colour, title): +def plot_scatter(f, xs, ys, size, pch, colour, title, title_align="center", xtitle="", ytitle=""): """ Form a complex number. @@ -61,6 +87,9 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): pch -- shape of the points (any character) colour -- colour of the points title -- title of the plot + title_align -- alignment for the title of the plot + xtitle -- title for x-axis + ytitle -- title for y-axis """ cs = None if f: @@ -81,7 +110,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): with open(ys) as fh: ys = [float(str(row).strip()) for row in fh] - _plot_scatter(xs, ys, size, pch, colour, title, cs) + _plot_scatter(xs, ys, size, pch, colour, title, title_align, cs, xtitle, ytitle) @@ -97,6 +126,8 @@ def main(): parser.add_option('-p', '--pch', help='shape of point', default="x", dest='pch') parser.add_option('-c', '--colour', help='colour of the plot (%s)' % colour_help, default='default', dest='colour') + parser.add_option('--xtitle', help='title for x-axis of graph', default='', dest='xtitle') + parser.add_option('--ytitle', help='title for y-axis of graph', default='', dest='ytitle') opts, args = parser.parse_args() diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index cf209ee..bf408d5 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -76,11 +76,22 @@ def abbreviate(labels, rfill=' '): return abbrev -def box_text(text, width, offset=0): +def box_text(text, title_align, width, offset=0): """ Return text inside an ascii textbox """ - box = " " * offset + "-" * (width+2) + "\n" - box += " " * offset + "|" + text.center(width) + "|" + "\n" - box += " " * offset + "-" * (width+2) + + if title_align == "right": + text = str.rjust(text, width) + elif title_align == "left": + text = str.ljust(text,width) + else: + text = text.center(width) + + if len(text) > width: + text = text[:width-3] + "..." + + box = " " * offset + "+" + "-" * (width) + "+" + "\n" + box += " " * offset + "|" + text + "|" + "\n" + box += " " * offset + "+" + "-" * (width) + "+" return box