From 5846494c2da9a9cac524491f6e798e4693727a57 Mon Sep 17 00:00:00 2001 From: cady Date: Thu, 11 Sep 2025 16:30:04 -0700 Subject: [PATCH 1/7] setup env and created simple graph --- scratch.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 scratch.py diff --git a/scratch.py b/scratch.py new file mode 100644 index 0000000..b671c71 --- /dev/null +++ b/scratch.py @@ -0,0 +1,18 @@ +# scratch.py +from bashplotlib.scatterplot import plot_scatter + +x_coords = [-10,20,30] +y_coords = [-10,20,30] +width = 10 +char = 'x' +color = 'default' +title = 'My Test Graph' + +plot_scatter( + None, + x_coords, + y_coords, + width, + char, + color, + title) From bd61113bedf503dc241d733e0f8a9f77a615e769 Mon Sep 17 00:00:00 2001 From: cady Date: Thu, 11 Sep 2025 16:56:31 -0700 Subject: [PATCH 2/7] made title box stylish --- bashplotlib/utils/helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index cf209ee..cd723ea 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -80,7 +80,7 @@ def box_text(text, width, offset=0): """ Return text inside an ascii textbox """ - box = " " * offset + "-" * (width+2) + "\n" + box = " " * offset + "+" + "-" * (width) + "+\n" box += " " * offset + "|" + text.center(width) + "|" + "\n" - box += " " * offset + "-" * (width+2) + box += " " * offset + "+" + "-" * (width) + "+" return box From 6418e7f186268d9c3537f904c00d1f9c00f75ef9 Mon Sep 17 00:00:00 2001 From: cady Date: Thu, 11 Sep 2025 17:19:11 -0700 Subject: [PATCH 3/7] added axis titles, made graphs prettier --- bashplotlib/scatterplot.py | 21 ++++++++++++++------- bashplotlib/utils/helpers.py | 6 +++--- scratch.py | 6 +++++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 69cab9d..27aa4bc 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -28,15 +28,19 @@ 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, cs, x_title, y_title): 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))) + print(" +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+") for y in get_scale(ys, True, size): - print("|", end=' ') + if y_title == "": + print(" |", end=' ') + else: + print(y_title[:1], "|", end=' ') + y_title = y_title[1:] for x in get_scale(xs, False, size): point = " " for (i, (xp, yp)) in enumerate(zip(xs, ys)): @@ -47,9 +51,10 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs): colour = cs[i] printcolour(point + " ", True, colour) print(" |") - print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + print(" +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+") + print(" ", x_title) -def plot_scatter(f, xs, ys, size, pch, colour, title): +def plot_scatter(f, xs, ys, size, pch, colour, title, x_title, y_title): """ Form a complex number. @@ -81,7 +86,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, cs, x_title, y_title) @@ -97,6 +102,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('-xt', '--xtitle', help='title for the x-axis', default="y axis", dest="x_title") + parser.add_option('-yt', '--ytitle', help='title for the y-axis', default="x axis", dest="y_title") opts, args = parser.parse_args() @@ -104,7 +111,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, opts.x_title, opts.y_title) else: print("nothing to plot!") diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index cd723ea..cd37a81 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -80,7 +80,7 @@ def box_text(text, width, offset=0): """ Return text inside an ascii textbox """ - box = " " * offset + "+" + "-" * (width) + "+\n" - box += " " * offset + "|" + text.center(width) + "|" + "\n" - box += " " * offset + "+" + "-" * (width) + "+" + box = " " * offset + " +" + "-" * (width) + "+\n" + box += " " * offset + " |" + text.center(width) + "|" + "\n" + box += " " * offset + " +" + "-" * (width) + "+" return box diff --git a/scratch.py b/scratch.py index b671c71..70a62d0 100644 --- a/scratch.py +++ b/scratch.py @@ -7,6 +7,8 @@ char = 'x' color = 'default' title = 'My Test Graph' +x_title = 'My X Axis' +y_title = 'My Y Axis' plot_scatter( None, @@ -15,4 +17,6 @@ width, char, color, - title) + title, + x_title, + y_title) From b6d55d690921b16cfa901df9ea19c5756aadc137 Mon Sep 17 00:00:00 2001 From: cady Date: Thu, 18 Sep 2025 17:47:01 -0700 Subject: [PATCH 4/7] added axes and origin in graph --- bashplotlib/scatterplot.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 27aa4bc..2b1de38 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -49,6 +49,14 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title): plotted.add((xp, yp)) if cs: colour = cs[i] + # print axes + if point != pch: + if x == 0.0 and y == 0.0: + point = "o" + elif x == 0.0: + point = "|" + elif y == 0.0: + point = "-" printcolour(point + " ", True, colour) print(" |") print(" +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+") From 24647583a1a61f1857fef7deb76897b9dacbe123 Mon Sep 17 00:00:00 2001 From: cady Date: Thu, 18 Sep 2025 17:59:24 -0700 Subject: [PATCH 5/7] made testable (_plot_scatter returns string of graph) --- bashplotlib/scatterplot.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 2b1de38..1f006af 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -30,16 +30,17 @@ def get_scale(series, is_y=False, steps=20): def _plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title): plotted = set() + graph = "" if title: - print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))) + graph += box_text(title, 2 * (len(get_scale(xs, False, size)) + 1)) + '\n' - print(" +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+") + graph += " +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+\n" for y in get_scale(ys, True, size): if y_title == "": - print(" |", end=' ') + graph += " | " else: - print(y_title[:1], "|", end=' ') + graph += y_title[:1] + " | " y_title = y_title[1:] for x in get_scale(xs, False, size): point = " " @@ -57,10 +58,11 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title): point = "|" elif y == 0.0: point = "-" - printcolour(point + " ", True, colour) - print(" |") - print(" +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+") - print(" ", x_title) + graph += point + " " + graph += " |\n" + graph += " +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+" + graph += " " + x_title + return graph def plot_scatter(f, xs, ys, size, pch, colour, title, x_title, y_title): """ @@ -94,7 +96,8 @@ def plot_scatter(f, xs, ys, size, pch, colour, title, x_title, y_title): with open(ys) as fh: ys = [float(str(row).strip()) for row in fh] - _plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title) + graph = _plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title) + print(graph) From 6c99a1f7e9c59ec5e1ae59fd8142640df8c8b8fe Mon Sep 17 00:00:00 2001 From: cady Date: Thu, 18 Sep 2025 18:14:01 -0700 Subject: [PATCH 6/7] fixed no coordinate error --- bashplotlib/scatterplot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 1f006af..0ca65ae 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -14,8 +14,11 @@ def get_scale(series, is_y=False, steps=20): - min_val = min(series) - max_val = max(series) + min_val = -1 * (steps / 2) + max_val = steps / 2 + if series != []: + min_val = min(series) + max_val = max(series) scaled_series = [] for x in drange(min_val, max_val, (max_val - min_val) / steps, include_stop=True): From f0dc7ea69a66c599bf0465e49cc2aae5758c85d2 Mon Sep 17 00:00:00 2001 From: cady Date: Thu, 18 Sep 2025 18:45:25 -0700 Subject: [PATCH 7/7] implemented basic unit tests --- bashplotlib/scatterplot.py | 6 ++-- test.py | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 test.py diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 0ca65ae..bcaa3c8 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -31,7 +31,7 @@ def get_scale(series, is_y=False, steps=20): return scaled_series -def _plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title): +def _plot_scatter(xs, ys, size, pch, title, cs, x_title, y_title): plotted = set() graph = "" @@ -63,7 +63,7 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title): point = "-" graph += point + " " graph += " |\n" - graph += " +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+" + graph += " +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+\n" graph += " " + x_title return graph @@ -99,7 +99,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title, x_title, y_title): with open(ys) as fh: ys = [float(str(row).strip()) for row in fh] - graph = _plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title) + graph = _plot_scatter(xs, ys, size, pch, title, cs, x_title, y_title) print(graph) diff --git a/test.py b/test.py new file mode 100644 index 0000000..6424074 --- /dev/null +++ b/test.py @@ -0,0 +1,69 @@ +# test.py +from bashplotlib.scatterplot import _plot_scatter +import unittest + +class TestScatterPlot(unittest.TestCase): + def test_no_points(self): + self.assertEqual(_plot_scatter([], [], 10, "x", + "My Test Graph", "default", "My X Axis", "My Y Axis"), + """ +------------------------+\n | My Test Graph | + +------------------------+ + +------------------------+ +M | | | +y | | | + | | | +Y | | | + | | | +A | - - - - - o - - - - - | +x | | | +i | | | +s | | | + | | | + | | | + +------------------------+ + My X Axis""") + + def test_points(self): + self.assertEqual(_plot_scatter([-10, -3, 20,30], [-10, 6, 20,-2], 10, "*", + "Points!", "default", "Something x", "Another y"), + """ +--------------------------+\n | Points! | + +--------------------------+ + +--------------------------+ +A | | * | +n | | | +o | | | +t | | | +h | | | +e | * | | +r | | | + | - - - o - - - - - - - - | +y | | | + | | * | + | | | + | * | | + +--------------------------+ + Something x""") + + def test_points_on_axes(self): + self.assertEqual(_plot_scatter([0, -10, 0, 20,0], [0, 0, 6, 0,-2], 10, "#", + "Points!", "default", "x", "y"), + """ +--------------------------+\n | Points! | + +--------------------------+ + +--------------------------+ +y | # | + | | | + | | | + | | | + | | | + | | | + | | | + | | | + | # - - - # - - - - - - # | + | | | + | | | + | # | + +--------------------------+ + x""") + +if __name__ == "__main__": + unittest.main()