From 9df8227fd4c8d27131999243794c3ebab995d181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Alexander=20Fellinghaug?= Date: Wed, 9 Sep 2015 12:19:03 +0200 Subject: [PATCH 1/3] PEP-8 and Python3 compatable python2 code * My Sublime Text 2 plugin auto-adjusted some small portions to be PEP-8 compliant. * Imported 'print_function' and refactored all print statements --- bashplotlib/histogram.py | 97 ++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 44 deletions(-) diff --git a/bashplotlib/histogram.py b/bashplotlib/histogram.py index bfff5b1..9c20101 100644 --- a/bashplotlib/histogram.py +++ b/bashplotlib/histogram.py @@ -5,6 +5,7 @@ Plotting terminal based histograms """ +from __future__ import print_function import os import sys import math @@ -55,32 +56,32 @@ def run_demo(): sys.stderr.write("run the downloaddata.sh script in the example first\n") sys.exit(1) - #plotting a histogram - print "plotting a basic histogram" - print "plot_hist('%s')" % demo_file - print "hist -f %s" % demo_file - print "cat %s | hist" % demo_file + # plotting a histogram + print("plotting a basic histogram") + print("plot_hist('%s')" % demo_file) + print("hist -f %s" % demo_file) + print("cat %s | hist" % demo_file) plot_hist(demo_file) - print "*" * 80 + print("*" * 80) - #with colours - print "histogram with colours" - print "plot_hist('%s', colour='blue')" % demo_file - print "hist -f %s -c blue" % demo_file + # with colours + print("histogram with colours") + print("plot_hist('%s', colour='blue')" % demo_file) + print("hist -f %s -c blue" % demo_file) plot_hist(demo_file, colour='blue') - print "*" * 80 + print("*" * 80) - #changing the shape of the point - print "changing the shape of the bars" - print "plot_hist('%s', pch='.')" % demo_file - print "hist -f %s -p ." % demo_file + # changing the shape of the point + print("changing the shape of the bars") + print("plot_hist('%s', pch='.')" % demo_file) + print("hist -f %s -p ." % demo_file) plot_hist(demo_file, pch='.') - print "*" * 80 + print("*" * 80) - #changing the size of the plot - print "changing the size of the plot" - print "plot_hist('%s', height=35.0, bincount=40)" % demo_file - print "hist -f %s -s 35.0 -b 40" % demo_file + # changing the size of the plot + print("changing the size of the plot") + print("plot_hist('%s', height=35.0, bincount=40)" % demo_file) + print("hist -f %s -s 35.0 -b 40" % demo_file) plot_hist(demo_file, height=35.0, bincount=40) @@ -139,13 +140,13 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def if height > 20: height = 20 - ys = list(drange(start, stop, float(stop - start)/height)) + ys = list(drange(start, stop, float(stop - start) / height)) ys.reverse() 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)), nlen)) print used_labs = set() @@ -155,64 +156,71 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def ylab = "" else: used_labs.add(ylab) - ylab = " "*(nlen - len(ylab)) + ylab + "|" + ylab = " " * (nlen - len(ylab)) + ylab + "|" - print ylab, + print(ylab, end=' ') for i in range(len(hist)): if int(y) <= hist[i]: printcolour(pch, True, colour) else: printcolour(" ", True, colour) - print + print() xs = hist.keys() - print " " * (nlen+1) + "-" * len(xs) + print(" " * (nlen + 1) + "-" * len(xs)) if xlab: - xlen = len(str(float((max_y)/height) + max_y)) + xlen = len(str(float((max_y) / height) + max_y)) for i in range(0, xlen): - printcolour(" " * (nlen+1), True, colour) + printcolour(" " * (nlen + 1), True, colour) for x in range(0, len(hist)): num = str(bins[x]) if x % 2 != 0: pass elif i < len(num): - print num[i], + print(num[i], end=' ') else: - print " ", + print(" ", end=' ') print center = max(map(len, map(str, [n, min_val, mean, max_val]))) center += 15 if showSummary: - print - print "-" * (2 + center) - print "|" + "Summary".center(center) + "|" - print "-" * (2 + center) + 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 += "|" + ("max value: %f" % max_val).center(center) + "|\n" summary += "-" * (2 + center) - print summary + print(summary) def main(): parser = optparse.OptionParser(usage=hist['usage']) - parser.add_option('-f', '--file', help='a file containing a column of numbers', default=None, dest='f') + parser.add_option( + '-f', '--file', help='a file containing a column of numbers', default=None, dest='f') parser.add_option('-t', '--title', help='title for the chart', default="", dest='t') - parser.add_option('-b', '--bins', help='number of bins in the histogram', type='int', default=None, dest='b') - parser.add_option('-w', '--binwidth', help='width of bins in the histogram', type='float', default=None, dest='binwidth') - parser.add_option('-s', '--height', help='height of the histogram (in lines)', type='int', default=None, dest='h') + parser.add_option( + '-b', '--bins', help='number of bins in the histogram', type='int', default=None, dest='b') + parser.add_option('-w', '--binwidth', help='width of bins in the histogram', + type='float', default=None, dest='binwidth') + parser.add_option('-s', '--height', help='height of the histogram (in lines)', + type='int', default=None, dest='h') parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p') - parser.add_option('-x', '--xlab', help='label bins on x-axis', default=None, action="store_true", dest='x') - parser.add_option('-c', '--colour', help='colour of the plot (%s)' % colour_help, default='default', dest='colour') + parser.add_option('-x', '--xlab', help='label bins on x-axis', + default=None, action="store_true", dest='x') + parser.add_option('-c', '--colour', help='colour of the plot (%s)' % + colour_help, default='default', dest='colour') parser.add_option('-d', '--demo', help='run demos', action='store_true', dest='demo') - parser.add_option('-n', '--nosummary', help='hide summary', action='store_false', dest='showSummary', default=True) + parser.add_option('-n', '--nosummary', help='hide summary', + action='store_false', dest='showSummary', default=True) parser.add_option('-r', '--regular', help='use regular y-scale (0 - maximum y value), instead of truncated y-scale (minimum y-value - maximum y-value)', default=False, action="store_true", dest='regular') @@ -228,9 +236,10 @@ def main(): if opts.demo: run_demo() elif opts.f: - plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary, opts.regular) + plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, + opts.t, opts.x, opts.showSummary, opts.regular) else: - print "nothing to plot!" + print("nothing to plot!") if __name__ == "__main__": From 5933873bf4ff0705fb1b610af942031248689274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Alexander=20Fellinghaug?= Date: Wed, 9 Sep 2015 13:41:34 +0200 Subject: [PATCH 2/3] Refactored to support Python3 and PEP-8 adjustments --- bashplotlib/histogram.py | 8 ++++--- bashplotlib/scatterplot.py | 48 ++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/bashplotlib/histogram.py b/bashplotlib/histogram.py index 9c20101..706aacb 100644 --- a/bashplotlib/histogram.py +++ b/bashplotlib/histogram.py @@ -6,6 +6,8 @@ """ from __future__ import print_function +from __future__ import division + import os import sys import math @@ -147,7 +149,7 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def if title: print(box_text(title, max(len(hist) * 2, len(title)), nlen)) - print + print() used_labs = set() for y in ys: @@ -165,7 +167,7 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def printcolour(pch, True, colour) else: printcolour(" ", True, colour) - print() + print('') xs = hist.keys() print(" " * (nlen + 1) + "-" * len(xs)) @@ -182,7 +184,7 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def print(num[i], end=' ') else: print(" ", end=' ') - print + print('') center = max(map(len, map(str, [n, min_val, mean, max_val]))) center += 15 diff --git a/bashplotlib/scatterplot.py b/bashplotlib/scatterplot.py index 23a49fd..ddaf3cc 100644 --- a/bashplotlib/scatterplot.py +++ b/bashplotlib/scatterplot.py @@ -5,22 +5,23 @@ Plotting terminal based scatterplots """ +from __future__ import print_function import csv import sys import optparse from .utils.helpers import * -from .utils.commandhelp import scatter +from .utils.commandhelp import scatter def get_scale(series, is_y=False, steps=20): min_val = min(series) max_val = max(series) scaled_series = [] - for x in drange(min_val, max_val, (max_val-min_val)/steps): + for x in drange(min_val, max_val, (max_val - min_val) / steps): if x > 0 and scaled_series and max(scaled_series) < 0: scaled_series.append(0.0) scaled_series.append(x) - + if is_y: scaled_series.reverse() return scaled_series @@ -29,7 +30,7 @@ def get_scale(series, is_y=False, steps=20): def plot_scatter(f, xs, ys, size, pch, colour, title): """ 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 @@ -37,13 +38,13 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): size -- size of the plot pch -- shape of the points (any character) colour -- colour of the points - title -- title of the plot + title -- title of the plot """ - + if f: if isinstance(f, str): f = open(f) - + data = [tuple(map(float, line.strip().split(','))) for line in f] xs = [i[0] for i in data] ys = [i[1] for i in data] @@ -52,29 +53,29 @@ def plot_scatter(f, xs, ys, size, pch, colour, title): ys = [float(str(row).strip()) for row in open(ys)] plotted = set() - + if title: - print box_text(title, 2*len(get_scale(xs, False, size))+1) - - print "-" * (2*len(get_scale(xs, False, size))+2) + print(box_text(title, 2 * len(get_scale(xs, False, size)) + 1)) + + print("-" * (2 * len(get_scale(xs, False, size)) + 2)) for y in get_scale(ys, True, size): - print "|", + print("|", end=' ') for x in get_scale(xs, False, size): point = " " for (i, (xp, yp)) in enumerate(zip(xs, ys)): if xp <= x and yp >= y and (xp, yp) not in plotted: point = pch - #point = str(i) + #point = str(i) plotted.add((xp, yp)) - if x==0 and y==0: + if x == 0 and y == 0: point = "o" - elif x==0: + elif x == 0: point = "|" - elif y==0: + elif y == 0: point = "-" printcolour(point, True, colour) - print "|" - print "-"*(2*len(get_scale(xs, False, size))+2) + print("|") + print("-" * (2 * len(get_scale(xs, False, size)) + 2)) def main(): @@ -85,9 +86,10 @@ def main(): parser.add_option('-t', '--title', help='title for the chart', default="", dest='t') parser.add_option('-x', help='x coordinates', default=None, dest='x') parser.add_option('-y', help='y coordinates', default=None, dest='y') - parser.add_option('-s', '--size',help='y coordinates', default=20, dest='size', type='int') - 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('-s', '--size', help='y coordinates', default=20, dest='size', type='int') + 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') opts, args = parser.parse_args() @@ -97,8 +99,8 @@ def main(): 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) else: - print "nothing to plot!" + print("nothing to plot!") -if __name__=="__main__": +if __name__ == "__main__": main() From 3ecdc2580d39b185300726960cbaf3ecf62bdedd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asbj=C3=B8rn=20Alexander=20Fellinghaug?= Date: Wed, 9 Sep 2015 13:42:17 +0200 Subject: [PATCH 3/3] bugfix: Did not read/open the textfile. Fixed now. --- bashplotlib/histogram.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bashplotlib/histogram.py b/bashplotlib/histogram.py index 706aacb..04a5a91 100644 --- a/bashplotlib/histogram.py +++ b/bashplotlib/histogram.py @@ -105,6 +105,9 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def if pch is None: pch = "o" + if isinstance(f, str): + f = open(f).readlines() + min_val, max_val = None, None n, mean = 0.0, 0.0