Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OSAB] Task 2: Allow users to pass titles for the x- and y-axes #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions bashplotlib/scatterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@ 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, xtitle, ytitle):
plotted = set()

x_scale = get_scale(xs, False, size)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assigned these scales to variables because we were calling the get_scale function a lot and it was getting a bit messy.

y_scale = get_scale(ys, True, size)
# Each x scale point gets 2 columns, then we need 2 more for the
# lines on the edge
plot_width = 2 * len(x_scale) + 2

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

if ytitle:
print("y: " + ytitle)

print("-" * (2 * (len(get_scale(xs, False, size)) + 2)))
for y in get_scale(ys, True, size):
print("+" + "-" * plot_width + "+")
for y in y_scale:
print("|", end=' ')
for x in get_scale(xs, False, size):
for x in x_scale:
point = " "
for (i, (xp, yp)) in enumerate(zip(xs, ys)):
if xp <= x and yp >= y and (xp, yp) not in plotted:
Expand All @@ -47,9 +56,13 @@ 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("+" + "-" * plot_width + "+")

if xtitle:
text = "x: " + xtitle
print(" " * (plot_width + 2 - len(text)) + text)

def plot_scatter(f, xs, ys, size, pch, colour, title):
def plot_scatter(f, xs, ys, size, pch, colour, title, xtitle=None, ytitle=None):
"""
Form a complex number.

Expand All @@ -61,6 +74,8 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
pch -- shape of the points (any character)
colour -- colour of the points
title -- title of the plot
xtitle -- the title of the x-axis
ytitle -- the title of the y-axis
"""
cs = None
if f:
Expand All @@ -81,7 +96,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, xtitle, ytitle)



Expand All @@ -97,14 +112,16 @@ 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('-X', '--xtitle', help='title for the x-axis', default=None)
parser.add_option('-Y', '--ytitle', help='title for the y-axis', default=None)

opts, args = parser.parse_args()

if opts.f is None and (opts.x is None or opts.y is None):
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.xtitle, opts.ytitle)
else:
print("nothing to plot!")

Expand Down