From 2df14f0a1dc85769c832f8f2aff777dfcf022973 Mon Sep 17 00:00:00 2001 From: Darren Date: Fri, 23 Dec 2022 20:57:11 +0800 Subject: [PATCH 01/14] new file: scratch.py test function in bashplotlib --- 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..dae66ac --- /dev/null +++ b/scratch.py @@ -0,0 +1,18 @@ +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 +) \ No newline at end of file From e6ef3700e21002925ea45b8be579a6e48c12eda6 Mon Sep 17 00:00:00 2001 From: Darren Date: Sat, 24 Dec 2022 13:48:37 +0800 Subject: [PATCH 02/14] modified: bashplotlib/utils/helpers.py added a "+" in the box for the text box of the title --- 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..9e6492b 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 45addf863ec9ba6a5735f52bccac4fa7f26ab72f Mon Sep 17 00:00:00 2001 From: Darren Date: Sat, 24 Dec 2022 14:18:51 +0800 Subject: [PATCH 03/14] modified: bashplotlib/utils/helpers.py added function to left/right justify the title --- bashplotlib/utils/helpers.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index 9e6492b..776dbfa 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -76,11 +76,16 @@ def abbreviate(labels, rfill=' '): return abbrev -def box_text(text, width, offset=0): +def box_text(text, width, title_align = "c", offset=0): """ Return text inside an ascii textbox """ box = " " * offset + "+" + "-" * (width) + "+" + "\n" - box += " " * offset + "|" + text.center(width) + "|" + "\n" + if title_align == "c": + box += " " * offset + "|" + text.center(width) + "|" + "\n" + elif title_align == "l": + box += " " * offset + "|" + text.ljust(width) + "|" + "\n" + elif title_align == "r": + box += " " * offset + "|" + text.rjust(width) + "|" + "\n" box += " " * offset + "+" + "-" * (width) + "+" return box From bb4ace17fccd24d1cfbbaa88935020ec3f930ff8 Mon Sep 17 00:00:00 2001 From: Darren Date: Sat, 24 Dec 2022 14:37:36 +0800 Subject: [PATCH 04/14] modified: bashplotlib/utils/helpers.py does not overflow, replaced with ... --- bashplotlib/utils/helpers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index 776dbfa..953c8a1 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -80,6 +80,8 @@ def box_text(text, width, title_align = "c", offset=0): """ Return text inside an ascii textbox """ + if len(text) > width: + text = text[:width-3] + "..." box = " " * offset + "+" + "-" * (width) + "+" + "\n" if title_align == "c": box += " " * offset + "|" + text.center(width) + "|" + "\n" From 7f682fb47a66be8cb78f62269cc03a5f5d1d38d5 Mon Sep 17 00:00:00 2001 From: Darren Date: Sat, 24 Dec 2022 14:43:06 +0800 Subject: [PATCH 05/14] modified: bashplotlib/utils/helpers.py imporve the edge of the title box --- bashplotlib/utils/helpers.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index 953c8a1..0d1673e 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -82,12 +82,14 @@ def box_text(text, width, title_align = "c", offset=0): """ if len(text) > width: text = text[:width-3] + "..." - box = " " * offset + "+" + "-" * (width) + "+" + "\n" + box = " " * offset + "+" + "-" * (width+2) + "+" + "\n" + box += " " * offset + "|+" + "-" * (width) + "+|" + "\n" if title_align == "c": - box += " " * offset + "|" + text.center(width) + "|" + "\n" + box += " " * offset + "||" + text.center(width) + "||" + "\n" elif title_align == "l": - box += " " * offset + "|" + text.ljust(width) + "|" + "\n" + box += " " * offset + "||" + text.ljust(width) + "||" + "\n" elif title_align == "r": - box += " " * offset + "|" + text.rjust(width) + "|" + "\n" - box += " " * offset + "+" + "-" * (width) + "+" + box += " " * offset + "||" + text.rjust(width) + "||" + "\n" + box += " " * offset + "|+" + "-" * (width) + "+|" + "\n" + box += " " * offset + "+" + "-" * (width+2) + "+" return box From 9891a9c3bd670fc6d27eaa2bae8e5669521aa313 Mon Sep 17 00:00:00 2001 From: Darren Date: Sun, 25 Dec 2022 15:32:14 +0800 Subject: [PATCH 06/14] modified: bashplotlib/scatterplot.py modified: scratch.py Added titles for the axis --- bashplotlib/scatterplot.py | 14 ++++++++++---- scratch.py | 6 +++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 69cab9d..3b3dce1 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -28,12 +28,14 @@ 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, xs_title, ys_title): plotted = set() - + if title: print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))) + if ys_title != None and isinstance(ys_title, str): + print(f'y: {ys_title}') print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) for y in get_scale(ys, True, size): print("|", end=' ') @@ -48,8 +50,12 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs): printcolour(point + " ", True, colour) print(" |") print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + if xs_title != None and isinstance(xs_title, str): + print(" " * (2 * (len(get_scale(xs, False, size)) + 2) - len(xs_title) - 3) + + "x: " + xs_title) + -def plot_scatter(f, xs, ys, size, pch, colour, title): +def plot_scatter(f, xs, ys, size, pch, colour, title, xs_title = None, ys_title = None): """ Form a complex number. @@ -81,7 +87,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, xs_title, ys_title) diff --git a/scratch.py b/scratch.py index dae66ac..dbda29a 100644 --- a/scratch.py +++ b/scratch.py @@ -6,6 +6,8 @@ char = 'x' color = 'default' title = 'My Test Graph' +xs_title = "The X axis" +ys_title = "The Y axis" plot_scatter( None, @@ -14,5 +16,7 @@ width, char, color, - title + title, + xs_title, + ys_title ) \ No newline at end of file From 958ef7cec81246a12120e7a3329fe5aab1e0ed95 Mon Sep 17 00:00:00 2001 From: Darren Date: Mon, 26 Dec 2022 16:36:55 +0800 Subject: [PATCH 07/14] modified: bashplotlib/scatterplot.py added comments for 0 axis --- bashplotlib/scatterplot.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 3b3dce1..4eaa7bb 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -30,7 +30,13 @@ def get_scale(series, is_y=False, steps=20): def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): plotted = set() - + axis_x, axis_y, point_0 = False, False, False + if min(xs) < 0 and max(xs) > 0: + axis_x = True + if min(ys) < 0 and max(ys) > 0: + axis_y = True + if axis_y == True and axis_x == True: + point_0 =True if title: print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))) @@ -41,6 +47,7 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): print("|", end=' ') for x in get_scale(xs, False, size): point = " " + #add the 0 axis here for (i, (xp, yp)) in enumerate(zip(xs, ys)): if xp <= x and yp >= y and (xp, yp) not in plotted: point = pch @@ -62,7 +69,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title, xs_title = None, ys_title Arguments: f -- comma delimited file w/ x,y coordinates xs -- if f not specified this is a file w/ x coordinates - ys -- if f not specified this is a filew / y coordinates + ys -- if f not specified this is a file w/ y coordinates size -- size of the plot pch -- shape of the points (any character) colour -- colour of the points @@ -90,7 +97,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title, xs_title = None, ys_title _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title) - +# need to add option to add axis title in this def main(): parser = optparse.OptionParser(usage=scatter['usage']) From 78f2f3b5cfb46b861840b2d45673a908460eef99 Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 27 Dec 2022 18:15:30 +0800 Subject: [PATCH 08/14] modified: bashplotlib/scatterplot.py added the axes modified: scratch.py added colors to test --- bashplotlib/scatterplot.py | 27 +++++++++++++++++++++------ scratch.py | 2 +- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 4eaa7bb..690650b 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -30,11 +30,11 @@ def get_scale(series, is_y=False, steps=20): def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): plotted = set() - axis_x, axis_y, point_0 = False, False, False - if min(xs) < 0 and max(xs) > 0: - axis_x = True - if min(ys) < 0 and max(ys) > 0: + axis_x, axis_y, point_0, axis_x_count = False, False, False, True + if min(xs) < 0 and max(xs) > 0: axis_y = True + if min(ys) < 0 and max(ys) > 0: + axis_x = True if axis_y == True and axis_x == True: point_0 =True if title: @@ -45,17 +45,32 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) for y in get_scale(ys, True, size): print("|", end=' ') + axis_y_count = True for x in get_scale(xs, False, size): point = " " - #add the 0 axis here + #generate the x-axis(i.e. there are positive and negative y points) + if axis_x and y < 0 and axis_x_count: + point = "-" + if axis_y and x > 0 and axis_y_count: + point = "0" + axis_y_count = False + #generate the y-axis(i.e there are positive and negative x points) + elif axis_y and x > 0 and axis_y_count: + point = "|" + axis_y_count = False 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 cs: colour = cs[i] - printcolour(point + " ", True, colour) + if point == "|" or point == "-" or point == "0": + printcolour(point + " ", True, "default") + else: + printcolour(point + " ", True, colour) print(" |") + if y < 0: + axis_x_count = False print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) if xs_title != None and isinstance(xs_title, str): print(" " * (2 * (len(get_scale(xs, False, size)) + 2) - len(xs_title) - 3) diff --git a/scratch.py b/scratch.py index dbda29a..086a61f 100644 --- a/scratch.py +++ b/scratch.py @@ -4,7 +4,7 @@ y_coords = [-10, 20, 30] width = 10 char = 'x' -color = 'default' +color = 'red' title = 'My Test Graph' xs_title = "The X axis" ys_title = "The Y axis" From 867d4ed64db4727be9e6b49dead6fcfe6478dcff Mon Sep 17 00:00:00 2001 From: Darren Date: Sun, 1 Jan 2023 16:58:17 +0800 Subject: [PATCH 09/14] modified: bashplotlib/scatterplot.py added unit test but removed color printing function for now --- bashplotlib/scatterplot.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 690650b..bfcff1f 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -29,6 +29,7 @@ def get_scale(series, is_y=False, steps=20): def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): + graph = "" plotted = set() axis_x, axis_y, point_0, axis_x_count = False, False, False, True if min(xs) < 0 and max(xs) > 0: @@ -38,13 +39,13 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): if axis_y == True and axis_x == True: point_0 =True if title: - print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))) + graph += f'{box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))}\n' if ys_title != None and isinstance(ys_title, str): - print(f'y: {ys_title}') - print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + graph += f'y: {ys_title}\n' + graph += "-" * (2 * (len(get_scale(xs, False, size)) + 2)) + '\n' for y in get_scale(ys, True, size): - print("|", end=' ') + graph += "| " axis_y_count = True for x in get_scale(xs, False, size): point = " " @@ -64,17 +65,15 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): plotted.add((xp, yp)) if cs: colour = cs[i] - if point == "|" or point == "-" or point == "0": - printcolour(point + " ", True, "default") - else: - printcolour(point + " ", True, colour) - print(" |") + graph += point + " " + graph += " |\n" if y < 0: axis_x_count = False - print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + graph += ("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + '\n' if xs_title != None and isinstance(xs_title, str): - print(" " * (2 * (len(get_scale(xs, False, size)) + 2) - len(xs_title) - 3) - + "x: " + xs_title) + graph += (" " * (2 * (len(get_scale(xs, False, size)) + 2) - len(xs_title) - 3) + + "x: " + xs_title) + '\n' + return graph def plot_scatter(f, xs, ys, size, pch, colour, title, xs_title = None, ys_title = None): @@ -109,7 +108,12 @@ def plot_scatter(f, xs, ys, size, pch, colour, title, xs_title = None, ys_title with open(ys) as fh: ys = [float(str(row).strip()) for row in fh] - _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title) + graph = _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title) + #if point == "|" or point == "-" or point == "0": + #printcolour(point + " ", True, "default") + #else: + # printcolour(point + " ", True, colour) + print(graph) # need to add option to add axis title in this From fd71e2fcd9a1ea12f3ca7e016ed3d03ecc5b1a54 Mon Sep 17 00:00:00 2001 From: Darren Date: Mon, 2 Jan 2023 17:57:26 +0800 Subject: [PATCH 10/14] modified: bashplotlib/utils/helpers.py made the text box same size as the graph --- bashplotlib/scatterplot.py | 1 + bashplotlib/utils/helpers.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index bfcff1f..bac4701 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -38,6 +38,7 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): axis_x = True if axis_y == True and axis_x == True: point_0 =True + #print title box if title: graph += f'{box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))}\n' diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index 0d1673e..72362fe 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -80,6 +80,8 @@ def box_text(text, width, title_align = "c", offset=0): """ Return text inside an ascii textbox """ + #to account for modification to the text box + width -= 2 if len(text) > width: text = text[:width-3] + "..." box = " " * offset + "+" + "-" * (width+2) + "+" + "\n" From d23d3f2f03b119c2b66bd4d46774d913a6b62769 Mon Sep 17 00:00:00 2001 From: Darren Date: Mon, 2 Jan 2023 17:58:24 +0800 Subject: [PATCH 11/14] new file: test.py made the new test file, havent added test case --- test.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..99f4b70 --- /dev/null +++ b/test.py @@ -0,0 +1,22 @@ +from bashplotlib.scatterplot import plot_scatter + +x_coords = [-10, 0, 10] +y_coords = [-10, 0, 10] +width = 21 +char = 'x' +color = 'red' +title = 'My Test Graph' +xs_title = "The X axis" +ys_title = "The Y axis" + +plot_scatter( + None, + x_coords, + y_coords, + width, + char, + color, + title, + xs_title, + ys_title +) \ No newline at end of file From 8880a3f1306e8ed2d5b48ded656b166622bd68cd Mon Sep 17 00:00:00 2001 From: Darren Date: Tue, 3 Jan 2023 17:16:25 +0800 Subject: [PATCH 12/14] modified: bashplotlib/scatterplot.py made the axis code shorter modified: bashplotlib/utils/helpers.py improved the scale code so it worked with more numbers --- bashplotlib/scatterplot.py | 54 +++++++++++++++++++----------------- bashplotlib/utils/helpers.py | 5 ++-- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index bac4701..b2781ba 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -14,11 +14,13 @@ def get_scale(series, is_y=False, steps=20): + #get range of the values 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): + #adds a 0 if crosses axis if x > 0 and scaled_series and max(scaled_series) < 0: scaled_series.append(0.0) scaled_series.append(x) @@ -31,45 +33,47 @@ def get_scale(series, is_y=False, steps=20): def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): graph = "" plotted = set() - axis_x, axis_y, point_0, axis_x_count = False, False, False, True - if min(xs) < 0 and max(xs) > 0: - axis_y = True - if min(ys) < 0 and max(ys) > 0: - axis_x = True - if axis_y == True and axis_x == True: - point_0 =True + x_scale = get_scale(xs, False, size) + print(x_scale) + y_scale = get_scale(ys, True, size) + #'*2' is due to the each point having a space in between, '+2' is due to the box border + plot_width = 2 * (len(x_scale)+2) + #print title box if title: - graph += f'{box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))}\n' + graph += f'{box_text(title, 2 * (len(x_scale) + 1))}\n' + #print y-axis title if ys_title != None and isinstance(ys_title, str): graph += f'y: {ys_title}\n' - graph += "-" * (2 * (len(get_scale(xs, False, size)) + 2)) + '\n' + + #print the top of box around graph + graph += "-" * plot_width + '\n' + + #start looping through the whole graph for y in get_scale(ys, True, size): graph += "| " - axis_y_count = True for x in get_scale(xs, False, size): - point = " " - #generate the x-axis(i.e. there are positive and negative y points) - if axis_x and y < 0 and axis_x_count: - point = "-" - if axis_y and x > 0 and axis_y_count: - point = "0" - axis_y_count = False - #generate the y-axis(i.e there are positive and negative x points) - elif axis_y and x > 0 and axis_y_count: - point = "|" - axis_y_count = False + #track whether the coor is in that square, will be used later + found_coor = False for (i, (xp, yp)) in enumerate(zip(xs, ys)): if xp <= x and yp >= y and (xp, yp) not in plotted: - point = pch + found_coor = True plotted.add((xp, yp)) if cs: colour = cs[i] - graph += point + " " + if found_coor: + graph += pch + ' ' + else: + if x == 0.0 and y == 0.0: + graph += 'o ' + elif x == 0.0: + graph += '| ' + elif y == 0.0: + graph += '- ' + else: + graph += ' ' graph += " |\n" - if y < 0: - axis_x_count = False graph += ("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + '\n' if xs_title != None and isinstance(xs_title, str): graph += (" " * (2 * (len(get_scale(xs, False, size)) + 2) - len(xs_title) - 3) diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index 72362fe..85418d3 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -53,10 +53,11 @@ def drange(start, stop, step=1.0, include_stop=False): r = start if include_stop: - while r <= stop: - yield r + yield r + while r < stop: r += step r = round(r, 10) + yield r else: while r < stop: yield r From 1c28dc44a92355b1850bf13e297987ba2a13fee8 Mon Sep 17 00:00:00 2001 From: Darren Date: Sun, 15 Jan 2023 18:20:39 +0800 Subject: [PATCH 13/14] modified: bashplotlib/scatterplot.py modified: test.py Added basic test --- bashplotlib/scatterplot.py | 3 +- test.py | 109 +++++++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 7 deletions(-) diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index b2781ba..d585379 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -34,7 +34,6 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): graph = "" plotted = set() x_scale = get_scale(xs, False, size) - print(x_scale) y_scale = get_scale(ys, True, size) #'*2' is due to the each point having a space in between, '+2' is due to the box border plot_width = 2 * (len(x_scale)+2) @@ -77,7 +76,7 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs, xs_title, ys_title): graph += ("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + '\n' if xs_title != None and isinstance(xs_title, str): graph += (" " * (2 * (len(get_scale(xs, False, size)) + 2) - len(xs_title) - 3) - + "x: " + xs_title) + '\n' + + "x: " + xs_title) return graph diff --git a/test.py b/test.py index 99f4b70..6684a4f 100644 --- a/test.py +++ b/test.py @@ -1,22 +1,121 @@ -from bashplotlib.scatterplot import plot_scatter +from bashplotlib.scatterplot import _plot_scatter x_coords = [-10, 0, 10] y_coords = [-10, 0, 10] -width = 21 +width = 10 char = 'x' color = 'red' title = 'My Test Graph' xs_title = "The X axis" ys_title = "The Y axis" -plot_scatter( - None, +test_a = _plot_scatter( x_coords, y_coords, width, char, color, + title, + None, + xs_title, + ys_title +) +expected_result_a = '''+------------------------+ +|+----------------------+| +|| My Test Graph || +|+----------------------+| ++------------------------+ +y: The Y axis +-------------------------- +| | x | +| | | +| | | +| | | +| | | +| - - - - - x - - - - - | +| | | +| | | +| | | +| | | +| x | | +-------------------------- + x: The X axis''' + +test_b = _plot_scatter( + [-10, 3, 5, 10], + [-10, 0, 0, 10], + width, + char, + color, + title, + None, + xs_title, + ys_title +) + +expected_result_b = '''+------------------------+ +|+----------------------+| +|| My Test Graph || +|+----------------------+| ++------------------------+ +y: The Y axis +-------------------------- +| | x | +| | | +| | | +| | | +| | | +| - - - - - o - x x - - | +| | | +| | | +| | | +| | | +| x | | +-------------------------- + x: The X axis''' + +test_c = _plot_scatter( + [-10, 3, 3, 10], + [-10, 0, 0, 10], + width, + char, + color, title, + None, xs_title, ys_title -) \ No newline at end of file +) + +expected_result_c = '''+------------------------+ +|+----------------------+| +|| My Test Graph || +|+----------------------+| ++------------------------+ +y: The Y axis +-------------------------- +| | x | +| | | +| | | +| | | +| | | +| - - - - - o - x - - - | +| | | +| | | +| | | +| | | +| x | | +-------------------------- + x: The X axis''' + +if test_a == expected_result_a: + print('SUCCESS') +else: + print('FAILURE') +if test_b == expected_result_b: + print('SUCCESS') +else: + print('FAILURE') +if test_c == expected_result_c: + print('SUCCESS') +else: + print('FAILURE') \ No newline at end of file From 854319efd3d52d381b15a71be39b9a09d2e52512 Mon Sep 17 00:00:00 2001 From: Darren Date: Sat, 11 Feb 2023 16:19:12 +0800 Subject: [PATCH 14/14] modified: bashplotlib/histogram.py modified: bashplotlib/utils/helpers.py modified: scratch.py improved the text_box function to accept more than one line updated the histogram showSummary function to use the text_box function --- bashplotlib/histogram.py | 21 ++++++++------------- bashplotlib/utils/helpers.py | 36 ++++++++++++++++++++++++++++++------ scratch.py | 15 +++++++++++++-- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/bashplotlib/histogram.py b/bashplotlib/histogram.py index fe0a5f7..2067050 100644 --- a/bashplotlib/histogram.py +++ b/bashplotlib/histogram.py @@ -159,9 +159,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def nlen = max(len(str(min_y)), len(str(max_y))) + 1 if title: - print(box_text(title, max(len(hist) * 2, len(title)), nlen)) + print(box_text(title, max(len(hist) * 2, len(title)))) print() - used_labs = set() for y in ys: ylab = str(int(y)) @@ -203,17 +202,13 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def if showSummary: print() - print("-" * (2 + center)) - print("|" + "Summary".center(center) + "|") - print("-" * (2 + center)) - summary = "|" + ("observations: %d" % n).center(center) + "|\n" - summary += "|" + ("min value: %f" % min_val).center(center) + "|\n" - summary += "|" + ("mean : %f" % mean).center(center) + "|\n" - summary += "|" + ("std dev : %f" % sd).center(center) + "|\n" - summary += "|" + ("max value: %f" % max_val).center(center) + "|\n" - summary += "-" * (2 + center) - print(summary) - + summary = ["Summary", + "observations: %d" % n, + "min value: %f" % min_val, + "mean : %f" % mean, + "std dev : %f" % sd, + "max value: %f" % max_val] + print(box_text(summary, center, True)) def main(): diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index 85418d3..037cc16 100644 --- a/bashplotlib/utils/helpers.py +++ b/bashplotlib/utils/helpers.py @@ -77,22 +77,46 @@ def abbreviate(labels, rfill=' '): return abbrev -def box_text(text, width, title_align = "c", offset=0): +def box_text(text, width, separator = False, title_align = "c", offset=0): """ Return text inside an ascii textbox + + Arguments help: + text -- text to be placed in the box, can be single line or multiple line + width -- width of the box + separator -- option to separate the first line from the rest + title_align -- c for centre, r for right, l for left + offset -- how far the box would be offset from the left """ + #need to make sure that multi-line text is able to be processed + #maybe accept the text in list form + #if separator = True, separate the first line, can improve to choose which line to separate + #to account for modification to the text box width -= 2 - if len(text) > width: - text = text[:width-3] + "..." + + if isinstance(text, list): + title = text[0] + content = text[1:] + elif isinstance(text, str): + title = text + content = None + if len(title) > width: + title = title[:width-3] + "..." + box = " " * offset + "+" + "-" * (width+2) + "+" + "\n" box += " " * offset + "|+" + "-" * (width) + "+|" + "\n" if title_align == "c": - box += " " * offset + "||" + text.center(width) + "||" + "\n" + box += " " * offset + "||" + title.center(width) + "||" + "\n" elif title_align == "l": - box += " " * offset + "||" + text.ljust(width) + "||" + "\n" + box += " " * offset + "||" + title.ljust(width) + "||" + "\n" elif title_align == "r": - box += " " * offset + "||" + text.rjust(width) + "||" + "\n" + box += " " * offset + "||" + title.rjust(width) + "||" + "\n" + if content != None: + if separator: + box += " " * offset + "||" + "-" * (width) + "||" + "\n" + for i in content: + box += " " * offset + "||" + i.center(width) + "||" + "\n" box += " " * offset + "|+" + "-" * (width) + "+|" + "\n" box += " " * offset + "+" + "-" * (width+2) + "+" return box diff --git a/scratch.py b/scratch.py index 086a61f..e0bca52 100644 --- a/scratch.py +++ b/scratch.py @@ -1,6 +1,17 @@ from bashplotlib.scatterplot import plot_scatter +from bashplotlib.histogram import plot_hist -x_coords = [-10, 20, 30] +'''height = 20 +bincount = 5 +binwidth = 20''' + +plot_hist( + 'hist_data.txt', + title = "test histogram", + showSummary=True +) + +'''x_coords = [-10, 20, 30] y_coords = [-10, 20, 30] width = 10 char = 'x' @@ -19,4 +30,4 @@ title, xs_title, ys_title -) \ No newline at end of file +)'''