Skip to content

Commit 5764227

Browse files
committed
Merge pull request glamp#26 from asbjorn/python2to3
Python2to3
2 parents 0386d56 + 3ecdc25 commit 5764227

File tree

2 files changed

+85
-69
lines changed

2 files changed

+85
-69
lines changed

‎bashplotlib/histogram.py‎

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
Plotting terminal based histograms
66
"""
77

8+
from __future__ importprint_function
9+
from __future__ importdivision
10+
811
importos
912
importsys
1013
importmath
@@ -55,32 +58,32 @@ def run_demo():
5558
sys.stderr.write("run the downloaddata.sh script in the example first\n")
5659
sys.exit(1)
5760

58-
#plotting a histogram
59-
print"plotting a basic histogram"
60-
print"plot_hist('%s')"%demo_file
61-
print"hist -f %s"%demo_file
62-
print"cat %s | hist"%demo_file
61+
#plotting a histogram
62+
print("plotting a basic histogram")
63+
print("plot_hist('%s')"%demo_file)
64+
print("hist -f %s"%demo_file)
65+
print("cat %s | hist"%demo_file)
6366
plot_hist(demo_file)
64-
print"*"*80
67+
print("*"*80)
6568

66-
#with colours
67-
print"histogram with colours"
68-
print"plot_hist('%s', colour='blue')"%demo_file
69-
print"hist -f %s -c blue"%demo_file
69+
#with colours
70+
print("histogram with colours")
71+
print("plot_hist('%s', colour='blue')"%demo_file)
72+
print("hist -f %s -c blue"%demo_file)
7073
plot_hist(demo_file, colour='blue')
71-
print"*"*80
74+
print("*"*80)
7275

73-
#changing the shape of the point
74-
print"changing the shape of the bars"
75-
print"plot_hist('%s', pch='.')"%demo_file
76-
print"hist -f %s -p ."%demo_file
76+
#changing the shape of the point
77+
print("changing the shape of the bars")
78+
print("plot_hist('%s', pch='.')"%demo_file)
79+
print("hist -f %s -p ."%demo_file)
7780
plot_hist(demo_file, pch='.')
78-
print"*"*80
81+
print("*"*80)
7982

80-
#changing the size of the plot
81-
print"changing the size of the plot"
82-
print"plot_hist('%s', height=35.0, bincount=40)"%demo_file
83-
print"hist -f %s -s 35.0 -b 40"%demo_file
83+
#changing the size of the plot
84+
print("changing the size of the plot")
85+
print("plot_hist('%s', height=35.0, bincount=40)"%demo_file)
86+
print("hist -f %s -s 35.0 -b 40"%demo_file)
8487
plot_hist(demo_file, height=35.0, bincount=40)
8588

8689

@@ -102,6 +105,9 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
102105
ifpchisNone:
103106
pch="o"
104107

108+
ifisinstance(f, str):
109+
f=open(f).readlines()
110+
105111
min_val, max_val=None, None
106112
n, mean=0.0, 0.0
107113

@@ -139,14 +145,14 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
139145
ifheight>20:
140146
height=20
141147

142-
ys=list(drange(start, stop, float(stop-start)/height))
148+
ys=list(drange(start, stop, float(stop-start)/height))
143149
ys.reverse()
144150

145151
nlen=max(len(str(min_y)), len(str(max_y))) +1
146152

147153
iftitle:
148-
printbox_text(title, max(len(hist)*2, len(title)), nlen)
149-
print
154+
print(box_text(title, max(len(hist)*2, len(title)), nlen))
155+
print()
150156

151157
used_labs=set()
152158
foryinys:
@@ -155,64 +161,71 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
155161
ylab=""
156162
else:
157163
used_labs.add(ylab)
158-
ylab=" "*(nlen-len(ylab)) +ylab+"|"
164+
ylab=" "*(nlen-len(ylab)) +ylab+"|"
159165

160-
printylab,
166+
print(ylab,end=' ')
161167

162168
foriinrange(len(hist)):
163169
ifint(y) <=hist[i]:
164170
printcolour(pch, True, colour)
165171
else:
166172
printcolour(" ", True, colour)
167-
print
173+
print('')
168174
xs=hist.keys()
169175

170-
print" "* (nlen+1) +"-"*len(xs)
176+
print(" "* (nlen+1) +"-"*len(xs))
171177

172178
ifxlab:
173-
xlen=len(str(float((max_y)/height) +max_y))
179+
xlen=len(str(float((max_y)/height) +max_y))
174180
foriinrange(0, xlen):
175-
printcolour(" "* (nlen+1), True, colour)
181+
printcolour(" "* (nlen+1), True, colour)
176182
forxinrange(0, len(hist)):
177183
num=str(bins[x])
178184
ifx%2!=0:
179185
pass
180186
elifi<len(num):
181-
printnum[i],
187+
print(num[i],end=' ')
182188
else:
183-
print" ",
184-
print
189+
print(" ",end=' ')
190+
print('')
185191

186192
center=max(map(len, map(str, [n, min_val, mean, max_val])))
187193
center+=15
188194

189195
ifshowSummary:
190-
print
191-
print"-"* (2+center)
192-
print"|"+"Summary".center(center) +"|"
193-
print"-"* (2+center)
196+
print()
197+
print("-"* (2+center))
198+
print("|"+"Summary".center(center) +"|")
199+
print("-"* (2+center))
194200
summary="|"+ ("observations: %d"%n).center(center) +"|\n"
195201
summary+="|"+ ("min value: %f"%min_val).center(center) +"|\n"
196202
summary+="|"+ ("mean : %f"%mean).center(center) +"|\n"
197203
summary+="|"+ ("max value: %f"%max_val).center(center) +"|\n"
198204
summary+="-"* (2+center)
199-
printsummary
205+
print(summary)
200206

201207

202208
defmain():
203209

204210
parser=optparse.OptionParser(usage=hist['usage'])
205211

206-
parser.add_option('-f', '--file', help='a file containing a column of numbers', default=None, dest='f')
212+
parser.add_option(
213+
'-f', '--file', help='a file containing a column of numbers', default=None, dest='f')
207214
parser.add_option('-t', '--title', help='title for the chart', default="", dest='t')
208-
parser.add_option('-b', '--bins', help='number of bins in the histogram', type='int', default=None, dest='b')
209-
parser.add_option('-w', '--binwidth', help='width of bins in the histogram', type='float', default=None, dest='binwidth')
210-
parser.add_option('-s', '--height', help='height of the histogram (in lines)', type='int', default=None, dest='h')
215+
parser.add_option(
216+
'-b', '--bins', help='number of bins in the histogram', type='int', default=None, dest='b')
217+
parser.add_option('-w', '--binwidth', help='width of bins in the histogram',
218+
type='float', default=None, dest='binwidth')
219+
parser.add_option('-s', '--height', help='height of the histogram (in lines)',
220+
type='int', default=None, dest='h')
211221
parser.add_option('-p', '--pch', help='shape of each bar', default='o', dest='p')
212-
parser.add_option('-x', '--xlab', help='label bins on x-axis', default=None, action="store_true", dest='x')
213-
parser.add_option('-c', '--colour', help='colour of the plot (%s)'%colour_help, default='default', dest='colour')
222+
parser.add_option('-x', '--xlab', help='label bins on x-axis',
223+
default=None, action="store_true", dest='x')
224+
parser.add_option('-c', '--colour', help='colour of the plot (%s)'%
225+
colour_help, default='default', dest='colour')
214226
parser.add_option('-d', '--demo', help='run demos', action='store_true', dest='demo')
215-
parser.add_option('-n', '--nosummary', help='hide summary', action='store_false', dest='showSummary', default=True)
227+
parser.add_option('-n', '--nosummary', help='hide summary',
228+
action='store_false', dest='showSummary', default=True)
216229
parser.add_option('-r', '--regular',
217230
help='use regular y-scale (0 - maximum y value), instead of truncated y-scale (minimum y-value - maximum y-value)',
218231
default=False, action="store_true", dest='regular')
@@ -228,9 +241,10 @@ def main():
228241
ifopts.demo:
229242
run_demo()
230243
elifopts.f:
231-
plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary, opts.regular)
244+
plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour,
245+
opts.t, opts.x, opts.showSummary, opts.regular)
232246
else:
233-
print"nothing to plot!"
247+
print("nothing to plot!")
234248

235249

236250
if__name__=="__main__":

‎bashplotlib/scatterplot.py‎

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
Plotting terminal based scatterplots
66
"""
77

8+
from __future__ importprint_function
89
importcsv
910
importsys
1011
importoptparse
1112
from .utils.helpersimport*
12-
from .utils.commandhelpimportscatter
13+
from .utils.commandhelpimportscatter
1314

1415

1516
defget_scale(series, is_y=False, steps=20):
1617
min_val=min(series)
1718
max_val=max(series)
1819
scaled_series= []
19-
forxindrange(min_val, max_val, (max_val-min_val)/steps):
20+
forxindrange(min_val, max_val, (max_val-min_val)/steps):
2021
ifx>0andscaled_seriesandmax(scaled_series) <0:
2122
scaled_series.append(0.0)
2223
scaled_series.append(x)
23-
24+
2425
ifis_y:
2526
scaled_series.reverse()
2627
returnscaled_series
@@ -29,21 +30,21 @@ def get_scale(series, is_y=False, steps=20):
2930
defplot_scatter(f, xs, ys, size, pch, colour, title):
3031
"""
3132
Form a complex number.
32-
33+
3334
Arguments:
3435
f -- comma delimited file w/ x,y coordinates
3536
xs -- if f not specified this is a file w/ x coordinates
3637
ys -- if f not specified this is a filew / y coordinates
3738
size -- size of the plot
3839
pch -- shape of the points (any character)
3940
colour -- colour of the points
40-
title -- title of the plot
41+
title -- title of the plot
4142
"""
42-
43+
4344
iff:
4445
ifisinstance(f, str):
4546
f=open(f)
46-
47+
4748
data= [tuple(map(float, line.strip().split(','))) forlineinf]
4849
xs= [i[0] foriindata]
4950
ys= [i[1] foriindata]
@@ -52,29 +53,29 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
5253
ys= [float(str(row).strip()) forrowinopen(ys)]
5354

5455
plotted=set()
55-
56+
5657
iftitle:
57-
printbox_text(title, 2*len(get_scale(xs, False, size))+1)
58-
59-
print"-"* (2*len(get_scale(xs, False, size))+2)
58+
print(box_text(title, 2*len(get_scale(xs, False, size))+1))
59+
60+
print("-"* (2*len(get_scale(xs, False, size))+2))
6061
foryinget_scale(ys, True, size):
61-
print"|",
62+
print("|",end=' ')
6263
forxinget_scale(xs, False, size):
6364
point=" "
6465
for (i, (xp, yp)) inenumerate(zip(xs, ys)):
6566
ifxp<=xandyp>=yand (xp, yp) notinplotted:
6667
point=pch
67-
#point = str(i)
68+
#point = str(i)
6869
plotted.add((xp, yp))
69-
ifx==0andy==0:
70+
ifx==0andy==0:
7071
point="o"
71-
elifx==0:
72+
elifx==0:
7273
point="|"
73-
elify==0:
74+
elify==0:
7475
point="-"
7576
printcolour(point, True, colour)
76-
print"|"
77-
print"-"*(2*len(get_scale(xs, False, size))+2)
77+
print("|")
78+
print("-"* (2*len(get_scale(xs, False, size))+2))
7879

7980

8081
defmain():
@@ -85,9 +86,10 @@ def main():
8586
parser.add_option('-t', '--title', help='title for the chart', default="", dest='t')
8687
parser.add_option('-x', help='x coordinates', default=None, dest='x')
8788
parser.add_option('-y', help='y coordinates', default=None, dest='y')
88-
parser.add_option('-s', '--size',help='y coordinates', default=20, dest='size', type='int')
89-
parser.add_option('-p', '--pch',help='shape of point', default="x", dest='pch')
90-
parser.add_option('-c', '--colour', help='colour of the plot (%s)'%colour_help, default='default', dest='colour')
89+
parser.add_option('-s', '--size', help='y coordinates', default=20, dest='size', type='int')
90+
parser.add_option('-p', '--pch', help='shape of point', default="x", dest='pch')
91+
parser.add_option('-c', '--colour', help='colour of the plot (%s)'%
92+
colour_help, default='default', dest='colour')
9193

9294
opts, args=parser.parse_args()
9395

@@ -97,8 +99,8 @@ def main():
9799
ifopts.for (opts.xandopts.y):
98100
plot_scatter(opts.f, opts.x, opts.y, opts.size, opts.pch, opts.colour, opts.t)
99101
else:
100-
print"nothing to plot!"
102+
print("nothing to plot!")
101103

102104

103-
if__name__=="__main__":
105+
if__name__=="__main__":
104106
main()

0 commit comments

Comments
(0)