@@ -86,7 +86,9 @@ def run_demo():
8686 plot_hist (demo_file , height = 35.0 , bincount = 40 )
8787
8888
89- def plot_hist (f , height = 20.0 , bincount = None , binwidth = None , pch = "o" , colour = "default" , title = "" , xlab = None , showSummary = False , regular = False ):
89+ def plot_hist (f , height = 20.0 , bincount = None , binwidth = None , pch = "o" ,
90+ colour = "default" , title = "" , xlab = None , showSummary = False ,
91+ regular = False , print_func = print ):
9092 ''' Plot histogram.
9193 1801| oo
9294 1681| oo
@@ -121,7 +123,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
121123 whether or not to display a summary
122124 regular : boolean
123125 whether or not to start y-labels at 0
124-
126+ print_func : callable
127+ custom print function
125128 '''
126129 if pch is None :
127130 pch = "o"
@@ -178,8 +181,8 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
178181 nlen = max (len (str (min_y )), len (str (max_y ))) + 1
179182
180183 if title :
181- print (box_text (title , max (len (hist ) * 2 , len (title )), nlen ))
182- print ()
184+ print_func (box_text (title , max (len (hist ) * 2 , len (title )), nlen ))
185+ print_func ()
183186
184187 used_labs = set ()
185188 for y in ys :
@@ -190,47 +193,47 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o", colour="def
190193 used_labs .add (ylab )
191194 ylab = " " * (nlen - len (ylab )) + ylab + "|"
192195
193- print (ylab , end = ' ' )
196+ print_func (ylab , end = ' ' )
194197
195198 for i in range (len (hist )):
196199 if int (y ) <= hist [i ]:
197- printcolour (pch , True , colour )
200+ printcolour (pch , True , colour , print_func )
198201 else :
199- printcolour (" " , True , colour )
200- print ('' )
202+ printcolour (" " , True , colour , print_func )
203+ print_func ('' )
201204 xs = hist .keys ()
202205
203- print (" " * (nlen + 1 ) + "-" * len (xs ))
206+ print_func (" " * (nlen + 1 ) + "-" * len (xs ))
204207
205208 if xlab :
206209 xlen = len (str (float ((max_y ) / height ) + max_y ))
207210 for i in range (0 , xlen ):
208- printcolour (" " * (nlen + 1 ), True , colour )
211+ printcolour (" " * (nlen + 1 ), True , colour , print_func )
209212 for x in range (0 , len (hist )):
210213 num = str (bins [x ])
211214 if x % 2 != 0 :
212215 pass
213216 elif i < len (num ):
214- print (num [i ], end = ' ' )
217+ print_func (num [i ], end = ' ' )
215218 else :
216- print (" " , end = ' ' )
217- print ('' )
219+ print_func (" " , end = ' ' )
220+ print_func ('' )
218221
219222 center = max (map (len , map (str , [n , min_val , mean , max_val ])))
220223 center += 15
221224
222225 if showSummary :
223- print ()
224- print ("-" * (2 + center ))
225- print ("|" + "Summary" .center (center ) + "|" )
226- print ("-" * (2 + center ))
226+ print_func ()
227+ print_func ("-" * (2 + center ))
228+ print_func ("|" + "Summary" .center (center ) + "|" )
229+ print_func ("-" * (2 + center ))
227230 summary = "|" + ("observations: %d" % n ).center (center ) + "|\n "
228231 summary += "|" + ("min value: %f" % min_val ).center (center ) + "|\n "
229232 summary += "|" + ("mean : %f" % mean ).center (center ) + "|\n "
230233 summary += "|" + ("sd : %f" % sd ).center (center ) + "|\n "
231234 summary += "|" + ("max value: %f" % max_val ).center (center ) + "|\n "
232235 summary += "-" * (2 + center )
233- print (summary )
236+ print_func (summary )
234237
235238
236239def main ():
0 commit comments