Skip to content

Commit 7aee70e

Browse files
committed
[OSAB] Allow users to pass titles for the x- and y-axes
1 parent cded03a commit 7aee70e

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

bashplotlib/scatterplot.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,25 @@ def get_scale(series, is_y=False, steps=20):
2828
return scaled_series
2929

3030

31-
def _plot_scatter(xs, ys, size, pch, colour, title, cs):
31+
def _plot_scatter(xs, ys, size, pch, colour, title, cs, xtitle, ytitle):
3232
plotted = set()
3333

34+
x_scale = get_scale(xs, False, size)
35+
y_scale = get_scale(ys, True, size)
36+
# Each x scale point gets 2 columns, then we need 2 more for the
37+
# lines on the edge
38+
plot_width = 2 * len(x_scale) + 2
39+
3440
if title:
35-
print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1)))
41+
print(box_text(title, plot_width))
42+
43+
if ytitle:
44+
print("y: " + ytitle)
3645

37-
print("-" * (2 * (len(get_scale(xs, False, size)) + 2)))
38-
for y in get_scale(ys, True, size):
46+
print("+" + "-" * plot_width + "+")
47+
for y in y_scale:
3948
print("|", end=' ')
40-
for x in get_scale(xs, False, size):
49+
for x in x_scale:
4150
point = " "
4251
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
4352
if xp <= x and yp >= y and (xp, yp) not in plotted:
@@ -47,9 +56,13 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs):
4756
colour = cs[i]
4857
printcolour(point + " ", True, colour)
4958
print(" |")
50-
print("-" * (2 * (len(get_scale(xs, False, size)) + 2)))
59+
print("+" + "-" * plot_width + "+")
60+
61+
if xtitle:
62+
text = "x: " + xtitle
63+
print(" " * (plot_width + 2 - len(text)) + text)
5164

52-
def plot_scatter(f, xs, ys, size, pch, colour, title):
65+
def plot_scatter(f, xs, ys, size, pch, colour, title, xtitle=None, ytitle=None):
5366
"""
5467
Form a complex number.
5568
@@ -61,6 +74,8 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
6174
pch -- shape of the points (any character)
6275
colour -- colour of the points
6376
title -- title of the plot
77+
xtitle -- the title of the x-axis
78+
ytitle -- the title of the y-axis
6479
"""
6580
cs = None
6681
if f:
@@ -81,7 +96,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
8196
with open(ys) as fh:
8297
ys = [float(str(row).strip()) for row in fh]
8398

84-
_plot_scatter(xs, ys, size, pch, colour, title, cs)
99+
_plot_scatter(xs, ys, size, pch, colour, title, cs, xtitle, ytitle)
85100

86101

87102

@@ -97,14 +112,16 @@ def main():
97112
parser.add_option('-p', '--pch', help='shape of point', default="x", dest='pch')
98113
parser.add_option('-c', '--colour', help='colour of the plot (%s)' %
99114
colour_help, default='default', dest='colour')
115+
parser.add_option('-X', '--xtitle', help='title for the x-axis', default=None)
116+
parser.add_option('-Y', '--ytitle', help='title for the y-axis', default=None)
100117

101118
opts, args = parser.parse_args()
102119

103120
if opts.f is None and (opts.x is None or opts.y is None):
104121
opts.f = sys.stdin.readlines()
105122

106123
if opts.f or (opts.x and opts.y):
107-
plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t)
124+
plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t, opts.xtitle, opts.ytitle)
108125
else:
109126
print("nothing to plot!")
110127

0 commit comments

Comments
 (0)