@@ -88,7 +88,7 @@ def run_demo():
8888
8989def plot_hist (f , height = 20.0 , bincount = None , binwidth = None , pch = "o" ,
9090 colour = "default" , title = "" , xlab = None , showSummary = False ,
91- regular = False , print_func = print ):
91+ regular = False , return_str = False ):
9292 ''' Plot histogram.
9393 1801| oo
9494 1681| oo
@@ -123,12 +123,12 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o",
123123 whether or not to display a summary
124124 regular : boolean
125125 whether or not to start y-labels at 0
126- print_func : callable
127- custom print function
126+ return_str : boolean
127+ return string represent the plot or print it out, default: False
128128 '''
129129 if pch is None :
130130 pch = "o"
131-
131+ splot = ''
132132 if isinstance (f , str ):
133133 f = open (f ).readlines ()
134134
@@ -181,8 +181,10 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o",
181181 nlen = max (len (str (min_y )), len (str (max_y ))) + 1
182182
183183 if title :
184- print_func (box_text (title , max (len (hist ) * 2 , len (title )), nlen ))
185- print_func ()
184+ splot += print_return_str (
185+ box_text (title , max (len (hist ) * 2 , len (title )), nlen ),
186+ return_str = return_str )
187+ splot += print_return_str ('' , return_str = return_str )
186188
187189 used_labs = set ()
188190 for y in ys :
@@ -193,47 +195,52 @@ def plot_hist(f, height=20.0, bincount=None, binwidth=None, pch="o",
193195 used_labs .add (ylab )
194196 ylab = " " * (nlen - len (ylab )) + ylab + "|"
195197
196- print_func (ylab , end = ' ' )
198+ splot += print_return_str (ylab , end = ' ' , return_str = return_str )
197199
198200 for i in range (len (hist )):
199201 if int (y ) <= hist [i ]:
200- printcolour (pch , True , colour , print_func )
202+ splot += printcolour (pch , True , colour , return_str )
201203 else :
202- printcolour (" " , True , colour , print_func )
203- print_func ('' )
204+ splot += printcolour (" " , True , colour , return_str )
205+ splot += print_return_str ('' , return_str = return_str )
204206 xs = hist .keys ()
205207
206- print_func (" " * (nlen + 1 ) + "-" * len (xs ))
208+ splot += print_return_str (" " * (nlen + 1 ) + "-" * len (xs ),
209+ return_str = return_str )
207210
208211 if xlab :
209212 xlen = len (str (float ((max_y ) / height ) + max_y ))
210213 for i in range (0 , xlen ):
211- printcolour (" " * (nlen + 1 ), True , colour , print_func )
214+ splot += printcolour (" " * (nlen + 1 ), True , colour , return_str )
212215 for x in range (0 , len (hist )):
213216 num = str (bins [x ])
214217 if x % 2 != 0 :
215218 pass
216219 elif i < len (num ):
217- print_func (num [i ], end = ' ' )
220+ splot += print_return_str (num [i ], end = ' ' ,
221+ return_str = return_str )
218222 else :
219- print_func (" " , end = ' ' )
220- print_func ('' )
223+ splot += print_return_str (" " , end = ' ' ,
224+ return_str = return_str )
225+ splot += print_return_str ('' , return_str = return_str )
221226
222227 center = max (map (len , map (str , [n , min_val , mean , max_val ])))
223228 center += 15
224229
225230 if showSummary :
226- print_func ()
227- print_func ("-" * (2 + center ))
228- print_func ("|" + "Summary" .center (center ) + "|" )
229- print_func ("-" * (2 + center ))
231+ splot += print_return_str ('' , return_str = return_str )
232+ splot += print_return_str ("-" * (2 + center ), return_str = return_str )
233+ splot += print_return_str ("|" + "Summary" .center (center ) + "|" ,
234+ return_str = return_str )
235+ splot += print_return_str ("-" * (2 + center ), return_str = return_str )
230236 summary = "|" + ("observations: %d" % n ).center (center ) + "|\n "
231237 summary += "|" + ("min value: %f" % min_val ).center (center ) + "|\n "
232238 summary += "|" + ("mean : %f" % mean ).center (center ) + "|\n "
233239 summary += "|" + ("sd : %f" % sd ).center (center ) + "|\n "
234240 summary += "|" + ("max value: %f" % max_val ).center (center ) + "|\n "
235241 summary += "-" * (2 + center )
236- print_func (summary )
242+ splot += print_return_str (summary , return_str = return_str )
243+ return splot
237244
238245
239246def main ():
0 commit comments