Skip to content

Python2to3 #26

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

Merged
merged 3 commits into from
Sep 12, 2015
Merged
Show file tree
Hide file tree
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
106 changes: 60 additions & 46 deletions bashplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
Plotting terminal based histograms
"""

from __future__ import print_function
from __future__ import division

import os
import sys
import math
Expand Down Expand Up @@ -55,32 +58,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)


Expand All @@ -102,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

Expand Down Expand Up @@ -139,14 +145,14 @@ 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
print(box_text(title, max(len(hist) * 2, len(title)), nlen))
print()

used_labs = set()
for y in ys:
Expand All @@ -155,64 +161,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
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')
Expand All @@ -228,9 +241,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__":
Expand Down
48 changes: 25 additions & 23 deletions bashplotlib/scatterplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,21 +30,21 @@ 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
ys -- if f not specified this is a filew / y coordinates
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]
Expand All @@ -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():
Expand All @@ -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()

Expand All @@ -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()