5
5
Plotting terminal based histograms
6
6
"""
7
7
8
+ from __future__ import print_function
9
+ from __future__ import division
10
+
8
11
import os
9
12
import sys
10
13
import math
@@ -55,32 +58,32 @@ def run_demo():
55
58
sys .stderr .write ("run the downloaddata.sh script in the example first\n " )
56
59
sys .exit (1 )
57
60
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 )
63
66
plot_hist (demo_file )
64
- print "*" * 80
67
+ print ( "*" * 80 )
65
68
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 )
70
73
plot_hist (demo_file , colour = 'blue' )
71
- print "*" * 80
74
+ print ( "*" * 80 )
72
75
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 )
77
80
plot_hist (demo_file , pch = '.' )
78
- print "*" * 80
81
+ print ( "*" * 80 )
79
82
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 )
84
87
plot_hist (demo_file , height = 35.0 , bincount = 40 )
85
88
86
89
@@ -102,6 +105,9 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
102
105
if pch is None :
103
106
pch = "o"
104
107
108
+ if isinstance (f , str ):
109
+ f = open (f ).readlines ()
110
+
105
111
min_val , max_val = None , None
106
112
n , mean = 0.0 , 0.0
107
113
@@ -139,14 +145,14 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
139
145
if height > 20 :
140
146
height = 20
141
147
142
- ys = list (drange (start , stop , float (stop - start )/ height ))
148
+ ys = list (drange (start , stop , float (stop - start ) / height ))
143
149
ys .reverse ()
144
150
145
151
nlen = max (len (str (min_y )), len (str (max_y ))) + 1
146
152
147
153
if title :
148
- print box_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 ()
150
156
151
157
used_labs = set ()
152
158
for y in ys :
@@ -155,64 +161,71 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
155
161
ylab = ""
156
162
else :
157
163
used_labs .add (ylab )
158
- ylab = " " * (nlen - len (ylab )) + ylab + "|"
164
+ ylab = " " * (nlen - len (ylab )) + ylab + "|"
159
165
160
- print ylab ,
166
+ print ( ylab , end = ' ' )
161
167
162
168
for i in range (len (hist )):
163
169
if int (y ) <= hist [i ]:
164
170
printcolour (pch , True , colour )
165
171
else :
166
172
printcolour (" " , True , colour )
167
- print
173
+ print ( '' )
168
174
xs = hist .keys ()
169
175
170
- print " " * (nlen + 1 ) + "-" * len (xs )
176
+ print ( " " * (nlen + 1 ) + "-" * len (xs ) )
171
177
172
178
if xlab :
173
- xlen = len (str (float ((max_y )/ height ) + max_y ))
179
+ xlen = len (str (float ((max_y ) / height ) + max_y ))
174
180
for i in range (0 , xlen ):
175
- printcolour (" " * (nlen + 1 ), True , colour )
181
+ printcolour (" " * (nlen + 1 ), True , colour )
176
182
for x in range (0 , len (hist )):
177
183
num = str (bins [x ])
178
184
if x % 2 != 0 :
179
185
pass
180
186
elif i < len (num ):
181
- print num [i ],
187
+ print ( num [i ], end = ' ' )
182
188
else :
183
- print " " ,
184
- print
189
+ print ( " " , end = ' ' )
190
+ print ( '' )
185
191
186
192
center = max (map (len , map (str , [n , min_val , mean , max_val ])))
187
193
center += 15
188
194
189
195
if showSummary :
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 ) )
194
200
summary = "|" + ("observations: %d" % n ).center (center ) + "|\n "
195
201
summary += "|" + ("min value: %f" % min_val ).center (center ) + "|\n "
196
202
summary += "|" + ("mean : %f" % mean ).center (center ) + "|\n "
197
203
summary += "|" + ("max value: %f" % max_val ).center (center ) + "|\n "
198
204
summary += "-" * (2 + center )
199
- print summary
205
+ print ( summary )
200
206
201
207
202
208
def main ():
203
209
204
210
parser = optparse .OptionParser (usage = hist ['usage' ])
205
211
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' )
207
214
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' )
211
221
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' )
214
226
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 )
216
229
parser .add_option ('-r' , '--regular' ,
217
230
help = 'use regular y-scale (0 - maximum y value), instead of truncated y-scale (minimum y-value - maximum y-value)' ,
218
231
default = False , action = "store_true" , dest = 'regular' )
@@ -228,9 +241,10 @@ def main():
228
241
if opts .demo :
229
242
run_demo ()
230
243
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 )
244
+ plot_hist (opts .f , opts .h , opts .b , opts .binwidth , opts .p , opts .colour ,
245
+ opts .t , opts .x , opts .showSummary , opts .regular )
232
246
else :
233
- print "nothing to plot!"
247
+ print ( "nothing to plot!" )
234
248
235
249
236
250
if __name__ == "__main__" :
0 commit comments