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/scatterplot.py b/bashplotlib/scatterplot.py index 69cab9d..d585379 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) @@ -28,35 +30,64 @@ 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): + graph = "" plotted = set() + x_scale = get_scale(xs, False, size) + 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: - print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1))) + graph += f'{box_text(title, 2 * (len(x_scale) + 1))}\n' - print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) + #print y-axis title + if ys_title != None and isinstance(ys_title, str): + graph += f'y: {ys_title}\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): - print("|", end=' ') + graph += "| " for x in get_scale(xs, False, size): - point = " " + #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] - printcolour(point + " ", True, colour) - print(" |") - print("-" * (2 * (len(get_scale(xs, False, size)) + 2))) - -def plot_scatter(f, xs, ys, size, pch, colour, title): + 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" + 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) + return graph + + +def plot_scatter(f, xs, ys, size, pch, colour, title, xs_title = None, ys_title = None): """ Form a complex number. 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 @@ -81,10 +112,15 @@ 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) + 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 def main(): parser = optparse.OptionParser(usage=scatter['usage']) diff --git a/bashplotlib/utils/helpers.py b/bashplotlib/utils/helpers.py index cf209ee..037cc16 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 @@ -76,11 +77,46 @@ def abbreviate(labels, rfill=' '): return abbrev -def box_text(text, width, 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 """ - box = " " * offset + "-" * (width+2) + "\n" - box += " " * offset + "|" + text.center(width) + "|" + "\n" - box += " " * offset + "-" * (width+2) + #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 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 + "||" + title.center(width) + "||" + "\n" + elif title_align == "l": + box += " " * offset + "||" + title.ljust(width) + "||" + "\n" + elif title_align == "r": + 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 new file mode 100644 index 0000000..e0bca52 --- /dev/null +++ b/scratch.py @@ -0,0 +1,33 @@ +from bashplotlib.scatterplot import plot_scatter +from bashplotlib.histogram import plot_hist + +'''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' +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 +)''' diff --git a/test.py b/test.py new file mode 100644 index 0000000..6684a4f --- /dev/null +++ b/test.py @@ -0,0 +1,121 @@ +from bashplotlib.scatterplot import _plot_scatter + +x_coords = [-10, 0, 10] +y_coords = [-10, 0, 10] +width = 10 +char = 'x' +color = 'red' +title = 'My Test Graph' +xs_title = "The X axis" +ys_title = "The Y axis" + +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 +) + +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