55Plotting terminal based histograms
66"""
77
8+ from __future__ import print_function
9+ from __future__ import division
10+
811import os
912import sys
1013import math
@@ -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 if pch is None :
103106 pch = "o"
104107
108+ if isinstance (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 if height > 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 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 ()
150156
151157 used_labs = set ()
152158 for y in ys :
@@ -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- print ylab ,
166+ print ( ylab , end = ' ' )
161167
162168 for i in range (len (hist )):
163169 if int (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 if xlab :
173- xlen = len (str (float ((max_y )/ height ) + max_y ))
179+ xlen = len (str (float ((max_y ) / height ) + max_y ))
174180 for i in range (0 , xlen ):
175- printcolour (" " * (nlen + 1 ), True , colour )
181+ printcolour (" " * (nlen + 1 ), True , colour )
176182 for x in range (0 , len (hist )):
177183 num = str (bins [x ])
178184 if x % 2 != 0 :
179185 pass
180186 elif i < len (num ):
181- print num [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 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 ) )
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- print summary
205+ print ( summary )
200206
201207
202208def main ():
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 if opts .demo :
229242 run_demo ()
230243 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 )
232246 else :
233- print "nothing to plot!"
247+ print ( "nothing to plot!" )
234248
235249
236250if __name__ == "__main__" :
0 commit comments