Skip to content

Commit 6418e7f

Browse files
committed
added axis titles, made graphs prettier
1 parent bd61113 commit 6418e7f

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

bashplotlib/scatterplot.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ 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, x_title, y_title):
3232
plotted = set()
3333

3434
if title:
3535
print(box_text(title, 2 * (len(get_scale(xs, False, size)) + 1)))
3636

37-
print("-" * (2 * (len(get_scale(xs, False, size)) + 2)))
37+
print(" +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+")
3838
for y in get_scale(ys, True, size):
39-
print("|", end=' ')
39+
if y_title == "":
40+
print(" |", end=' ')
41+
else:
42+
print(y_title[:1], "|", end=' ')
43+
y_title = y_title[1:]
4044
for x in get_scale(xs, False, size):
4145
point = " "
4246
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
@@ -47,9 +51,10 @@ def _plot_scatter(xs, ys, size, pch, colour, title, cs):
4751
colour = cs[i]
4852
printcolour(point + " ", True, colour)
4953
print(" |")
50-
print("-" * (2 * (len(get_scale(xs, False, size)) + 2)))
54+
print(" +" + "-" * (2 * (len(get_scale(xs, False, size)) + 2) - 2) + "+")
55+
print(" ", x_title)
5156

52-
def plot_scatter(f, xs, ys, size, pch, colour, title):
57+
def plot_scatter(f, xs, ys, size, pch, colour, title, x_title, y_title):
5358
"""
5459
Form a complex number.
5560
@@ -81,7 +86,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
8186
with open(ys) as fh:
8287
ys = [float(str(row).strip()) for row in fh]
8388

84-
_plot_scatter(xs, ys, size, pch, colour, title, cs)
89+
_plot_scatter(xs, ys, size, pch, colour, title, cs, x_title, y_title)
8590

8691

8792

@@ -97,14 +102,16 @@ def main():
97102
parser.add_option('-p', '--pch', help='shape of point', default="x", dest='pch')
98103
parser.add_option('-c', '--colour', help='colour of the plot (%s)' %
99104
colour_help, default='default', dest='colour')
105+
parser.add_option('-xt', '--xtitle', help='title for the x-axis', default="y axis", dest="x_title")
106+
parser.add_option('-yt', '--ytitle', help='title for the y-axis', default="x axis", dest="y_title")
100107

101108
opts, args = parser.parse_args()
102109

103110
if opts.f is None and (opts.x is None or opts.y is None):
104111
opts.f = sys.stdin.readlines()
105112

106113
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)
114+
plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t, opts.x_title, opts.y_title)
108115
else:
109116
print("nothing to plot!")
110117

bashplotlib/utils/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def box_text(text, width, offset=0):
8080
"""
8181
Return text inside an ascii textbox
8282
"""
83-
box = " " * offset + "+" + "-" * (width) + "+\n"
84-
box += " " * offset + "|" + text.center(width) + "|" + "\n"
85-
box += " " * offset + "+" + "-" * (width) + "+"
83+
box = " " * offset + " +" + "-" * (width) + "+\n"
84+
box += " " * offset + " |" + text.center(width) + "|" + "\n"
85+
box += " " * offset + " +" + "-" * (width) + "+"
8686
return box

scratch.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
char = 'x'
88
color = 'default'
99
title = 'My Test Graph'
10+
x_title = 'My X Axis'
11+
y_title = 'My Y Axis'
1012

1113
plot_scatter(
1214
None,
@@ -15,4 +17,6 @@
1517
width,
1618
char,
1719
color,
18-
title)
20+
title,
21+
x_title,
22+
y_title)

0 commit comments

Comments
 (0)