@@ -28,7 +28,58 @@ def get_scale(series, is_y=False, steps=20):
2828 return scaled_series
2929
3030
31- def plot_scatter (f , xs , ys , size , pch , colour , title ):
31+ def plot_scatter (xs , ys , size = None , pch = 'o' , colour = 'red' , title = None , print_func = print ):
32+ ''' Scatter plot.
33+ ----------------------
34+ | * |
35+ | * |
36+ | * |
37+ | * |
38+ | * |
39+ | * |
40+ | * |
41+ | * |
42+ -----------------------
43+ Parameters
44+ ----------
45+ xs : list, numpy.ndarray
46+ list of x series
47+ ys : list, numpy.ndarray
48+ list of y series
49+ size : int
50+ width of plot
51+ pch : str
52+ any character to represent a points
53+ colour : str, list(str)
54+ white,aqua,pink,blue,yellow,green,red,grey,black,default,ENDC
55+ title : str
56+ title for the plot, None = not show
57+ print_func : callable
58+ function for print out a string
59+
60+ '''
61+ plotted = set ()
62+ cs = colour
63+
64+ if title :
65+ print_func (box_text (title , 2 * len (get_scale (xs , False , size )) + 1 ))
66+
67+ print_func ("-" * (2 * len (get_scale (xs , False , size )) + 2 ))
68+ for y in get_scale (ys , True , size ):
69+ print_func ("|" , end = ' ' )
70+ for x in get_scale (xs , False , size ):
71+ point = " "
72+ for (i , (xp , yp )) in enumerate (zip (xs , ys )):
73+ if xp <= x and yp >= y and (xp , yp ) not in plotted :
74+ point = pch
75+ plotted .add ((xp , yp ))
76+ if isinstance (cs , list ):
77+ colour = cs [i ]
78+ printcolour (point , True , colour )
79+ print_func (" |" )
80+ print_func ("-" * (2 * len (get_scale (xs , False , size )) + 2 ))
81+
82+ def _plot_scatter (f , xs , ys , size , pch , colour , title ):
3283 """
3384 Form a complex number.
3485
@@ -60,27 +111,7 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
60111 cs = [str (row ).strip () for row in open (colour )]
61112 else :
62113 cs = colour
63-
64- plotted = set ()
65-
66- if title :
67- print (box_text (title , 2 * len (get_scale (xs , False , size )) + 1 ))
68-
69- print ("-" * (2 * len (get_scale (xs , False , size )) + 2 ))
70- for y in get_scale (ys , True , size ):
71- print ("|" , end = ' ' )
72- for x in get_scale (xs , False , size ):
73- point = " "
74- for (i , (xp , yp )) in enumerate (zip (xs , ys )):
75- if xp <= x and yp >= y and (xp , yp ) not in plotted :
76- point = pch
77- plotted .add ((xp , yp ))
78- if isinstance (cs , list ):
79- colour = cs [i ]
80- printcolour (point , True , colour )
81- print (" |" )
82- print ("-" * (2 * len (get_scale (xs , False , size )) + 2 ))
83-
114+ plot_scatter (xs , ys , size = size , pch = pch , colour = cs , title = title )
84115
85116def main ():
86117
@@ -101,7 +132,7 @@ def main():
101132 opts .f = sys .stdin .readlines ()
102133
103134 if opts .f or (opts .x and opts .y ):
104- plot_scatter (opts .f , opts .x , opts .y , opts .size , opts .pch , opts .colour , opts .t )
135+ _plot_scatter (opts .f , opts .x , opts .y , opts .size , opts .pch , opts .colour , opts .t )
105136 else :
106137 print ("nothing to plot!" )
107138
0 commit comments