@@ -14,16 +14,44 @@ def calc_bins(n, min_val, max_val, h=None):
1414 yield b
1515
1616def read_numbers (numbers ):
17+ "read the input data in the most optimal way"
1718 if isinstance (numbers , list ):
1819 for n in numbers :
20+ n = str (n )
1921 yield float (n .strip ())
2022 else :
2123 for n in open (numbers ):
2224 yield float (n .strip ())
2325
26+ def run_demo ():
27+ "demo the product"
28+ #plotting a histogram
29+ print "plotting a basic histogram"
30+ print "plot_hist('./data/exp.txt')"
31+ print "hist -f ./data/exp.txt"
32+ print "cat ./data/exp.txt | hist"
33+ plot_hist ('./data/exp.txt' )
34+ print "*" * 80
35+ #with colors
36+ print "histogram with colors"
37+ print "plot_hist('./data/exp.txt', colour='blue')"
38+ print "hist -f ./data/exp.txt -c blue"
39+ plot_hist ('./data/exp.txt' , colour = 'blue' )
40+ print "*" * 80
41+ #changing the shape of the point
42+ print "changing the shape of the bars"
43+ print "plot_hist('./data/exp.txt', pch='.')"
44+ print "hist -f ./data/exp.txt -p ."
45+ plot_hist ('./data/exp.txt' , pch = '.' )
46+ print "*" * 80
47+ #chagning the size of the plot
48+ print "chagning the size of the plot"
49+ print "plot_hist('./data/exp.txt', height=35.0, bincount=40)"
50+ print "hist -f ./data/exp.txt -s 35.0 -b 40"
51+ plot_hist ('./data/exp.txt' , height = 35.0 , bincount = 40 )
52+
2453def plot_hist (f , height = 20.0 , bincount = None , pch = "o" , colour = "white" , title = "" , xlab = None ):
2554 "plot a histogram given a file of numbers"
26- #first apss
2755 if pch is None :
2856 pch = "o"
2957
@@ -55,7 +83,9 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
5583 ys .reverse ()
5684
5785 nlen = max (len (str (min_y )), len (str (max_y ))) + 1
58- print title .center (len (hist ) + nlen + 1 )
86+
87+ if title :
88+ print box_text (title , len (hist )* 2 , nlen )
5989 print
6090 used_labs = set ()
6191 for y in ys :
@@ -119,16 +149,19 @@ def plot_hist(f, height=20.0, bincount=None, pch="o", colour="white", title="",
119149 parser .add_option ('-x' , '--xlab' , help = 'label bins on x-axis' , default = None , action = "store_true" , dest = 'x' )
120150 parser .add_option ('-c' , '--colour' , help = 'colour of the plot (%s)' % ", " .join ([c for c in bcolours .keys () if c != 'ENDC' ]),
121151 default = 'white' , dest = 'colour' )
152+ parser .add_option ('-d' , '--demo' , help = 'run demos' , action = 'store_true' , dest = 'demo' )
122153
123154 (opts , args ) = parser .parse_args ()
124155
125156 if opts .f is None :
126157 if len (args ) > 0 :
127158 opts .f = args [0 ]
128- else :
159+ elif opts . demo is False :
129160 opts .f = sys .stdin .readlines ()
130161
131- if opts .f :
162+ if opts .demo :
163+ run_demo ()
164+ elif opts .f :
132165 plot_hist (opts .f , opts .h , opts .b , opts .p , opts .colour , opts .t , opts .x )
133166 else :
134167 print "nothing to plot!"
0 commit comments