|
5 | 5 | Plotting terminal based histograms |
6 | 6 | """ |
7 | 7 |
|
| 8 | +from __future__ import print_function |
8 | 9 | import os |
9 | 10 | import sys |
10 | 11 | import math |
@@ -55,32 +56,32 @@ def run_demo(): |
55 | 56 | sys.stderr.write("run the downloaddata.sh script in the example first\n") |
56 | 57 | sys.exit(1) |
57 | 58 |
|
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 |
| 59 | + # plotting a histogram |
| 60 | + print("plotting a basic histogram") |
| 61 | + print("plot_hist('%s')" % demo_file) |
| 62 | + print("hist -f %s" % demo_file) |
| 63 | + print("cat %s | hist" % demo_file) |
63 | 64 | plot_hist(demo_file) |
64 | | - print "*" * 80 |
| 65 | + print("*" * 80) |
65 | 66 |
|
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 |
| 67 | + # with colours |
| 68 | + print("histogram with colours") |
| 69 | + print("plot_hist('%s', colour='blue')" % demo_file) |
| 70 | + print("hist -f %s -c blue" % demo_file) |
70 | 71 | plot_hist(demo_file, colour='blue') |
71 | | - print "*" * 80 |
| 72 | + print("*" * 80) |
72 | 73 |
|
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 |
| 74 | + # changing the shape of the point |
| 75 | + print("changing the shape of the bars") |
| 76 | + print("plot_hist('%s', pch='.')" % demo_file) |
| 77 | + print("hist -f %s -p ." % demo_file) |
77 | 78 | plot_hist(demo_file, pch='.') |
78 | | - print "*" * 80 |
| 79 | + print("*" * 80) |
79 | 80 |
|
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 |
| 81 | + # changing the size of the plot |
| 82 | + print("changing the size of the plot") |
| 83 | + print("plot_hist('%s', height=35.0, bincount=40)" % demo_file) |
| 84 | + print("hist -f %s -s 35.0 -b 40" % demo_file) |
84 | 85 | plot_hist(demo_file, height=35.0, bincount=40) |
85 | 86 |
|
86 | 87 |
|
@@ -139,13 +140,13 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def |
139 | 140 | if height > 20: |
140 | 141 | height = 20 |
141 | 142 |
|
142 | | - ys = list(drange(start, stop, float(stop - start)/height)) |
| 143 | + ys = list(drange(start, stop, float(stop - start) / height)) |
143 | 144 | ys.reverse() |
144 | 145 |
|
145 | 146 | nlen = max(len(str(min_y)), len(str(max_y))) + 1 |
146 | 147 |
|
147 | 148 | if title: |
148 | | - print box_text(title, max(len(hist)*2, len(title)), nlen) |
| 149 | + print(box_text(title, max(len(hist) * 2, len(title)), nlen)) |
149 | 150 | print |
150 | 151 |
|
151 | 152 | used_labs = set() |
@@ -155,64 +156,71 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def |
155 | 156 | ylab = "" |
156 | 157 | else: |
157 | 158 | used_labs.add(ylab) |
158 | | - ylab = " "*(nlen - len(ylab)) + ylab + "|" |
| 159 | + ylab = " " * (nlen - len(ylab)) + ylab + "|" |
159 | 160 |
|
160 | | - print ylab, |
| 161 | + print(ylab, end=' ') |
161 | 162 |
|
162 | 163 | for i in range(len(hist)): |
163 | 164 | if int(y) <= hist[i]: |
164 | 165 | printcolour(pch, True, colour) |
165 | 166 | else: |
166 | 167 | printcolour(" ", True, colour) |
167 | | - print |
| 168 | + print() |
168 | 169 | xs = hist.keys() |
169 | 170 |
|
170 | | - print " " * (nlen+1) + "-" * len(xs) |
| 171 | + print(" " * (nlen + 1) + "-" * len(xs)) |
171 | 172 |
|
172 | 173 | if xlab: |
173 | | - xlen = len(str(float((max_y)/height) + max_y)) |
| 174 | + xlen = len(str(float((max_y) / height) + max_y)) |
174 | 175 | for i in range(0, xlen): |
175 | | - printcolour(" " * (nlen+1), True, colour) |
| 176 | + printcolour(" " * (nlen + 1), True, colour) |
176 | 177 | for x in range(0, len(hist)): |
177 | 178 | num = str(bins[x]) |
178 | 179 | if x % 2 != 0: |
179 | 180 | pass |
180 | 181 | elif i < len(num): |
181 | | - print num[i], |
| 182 | + print(num[i], end=' ') |
182 | 183 | else: |
183 | | - print " ", |
| 184 | + print(" ", end=' ') |
184 | 185 | print |
185 | 186 |
|
186 | 187 | center = max(map(len, map(str, [n, min_val, mean, max_val]))) |
187 | 188 | center += 15 |
188 | 189 |
|
189 | 190 | if showSummary: |
190 | | - print |
191 | | - print "-" * (2 + center) |
192 | | - print "|" + "Summary".center(center) + "|" |
193 | | - print "-" * (2 + center) |
| 191 | + print() |
| 192 | + print("-" * (2 + center)) |
| 193 | + print("|" + "Summary".center(center) + "|") |
| 194 | + print("-" * (2 + center)) |
194 | 195 | summary = "|" + ("observations: %d" % n).center(center) + "|\n" |
195 | 196 | summary += "|" + ("min value: %f" % min_val).center(center) + "|\n" |
196 | 197 | summary += "|" + ("mean : %f" % mean).center(center) + "|\n" |
197 | 198 | summary += "|" + ("max value: %f" % max_val).center(center) + "|\n" |
198 | 199 | summary += "-" * (2 + center) |
199 | | - print summary |
| 200 | + print(summary) |
200 | 201 |
|
201 | 202 |
|
202 | 203 | def main(): |
203 | 204 |
|
204 | 205 | parser = optparse.OptionParser(usage=hist['usage']) |
205 | 206 |
|
206 | | - parser.add_option('-f', '--file', help='a file containing a column of numbers', default=None, dest='f') |
| 207 | + parser.add_option( |
| 208 | + '-f', '--file', help='a file containing a column of numbers', default=None, dest='f') |
207 | 209 | 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') |
| 210 | + parser.add_option( |
| 211 | + '-b', '--bins', help='number of bins in the histogram', type='int', default=None, dest='b') |
| 212 | + parser.add_option('-w', '--binwidth', help='width of bins in the histogram', |
| 213 | + type='float', default=None, dest='binwidth') |
| 214 | + parser.add_option('-s', '--height', help='height of the histogram (in lines)', |
| 215 | + type='int', default=None, dest='h') |
211 | 216 | 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') |
| 217 | + parser.add_option('-x', '--xlab', help='label bins on x-axis', |
| 218 | + default=None, action="store_true", dest='x') |
| 219 | + parser.add_option('-c', '--colour', help='colour of the plot (%s)' % |
| 220 | + colour_help, default='default', dest='colour') |
214 | 221 | 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) |
| 222 | + parser.add_option('-n', '--nosummary', help='hide summary', |
| 223 | + action='store_false', dest='showSummary', default=True) |
216 | 224 | parser.add_option('-r', '--regular', |
217 | 225 | help='use regular y-scale (0 - maximum y value), instead of truncated y-scale (minimum y-value - maximum y-value)', |
218 | 226 | default=False, action="store_true", dest='regular') |
@@ -228,9 +236,10 @@ def main(): |
228 | 236 | if opts.demo: |
229 | 237 | run_demo() |
230 | 238 | elif opts.f: |
231 | | - plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, opts.t, opts.x, opts.showSummary, opts.regular) |
| 239 | + plot_hist(opts.f, opts.h, opts.b, opts.binwidth, opts.p, opts.colour, |
| 240 | + opts.t, opts.x, opts.showSummary, opts.regular) |
232 | 241 | else: |
233 | | - print "nothing to plot!" |
| 242 | + print("nothing to plot!") |
234 | 243 |
|
235 | 244 |
|
236 | 245 | if __name__ == "__main__": |
|