55Plotting terminal based scatterplots
66"""
77
8+ from __future__ import print_function
89import csv
910import sys
1011import optparse
1112from .utils .helpers import *
12- from .utils .commandhelp import scatter
13+ from .utils .commandhelp import scatter
1314
1415
1516def get_scale (series , is_y = False , steps = 20 ):
1617 min_val = min (series )
1718 max_val = max (series )
1819 scaled_series = []
19- for x in drange (min_val , max_val , (max_val - min_val )/ steps ):
20+ for x in drange (min_val , max_val , (max_val - min_val ) / steps ):
2021 if x > 0 and scaled_series and max (scaled_series ) < 0 :
2122 scaled_series .append (0.0 )
2223 scaled_series .append (x )
23-
24+
2425 if is_y :
2526 scaled_series .reverse ()
2627 return scaled_series
@@ -29,21 +30,21 @@ def get_scale(series, is_y=False, steps=20):
2930def plot_scatter (f , xs , ys , size , pch , colour , title ):
3031 """
3132 Form a complex number.
32-
33+
3334 Arguments:
3435 f -- comma delimited file w/ x,y coordinates
3536 xs -- if f not specified this is a file w/ x coordinates
3637 ys -- if f not specified this is a filew / y coordinates
3738 size -- size of the plot
3839 pch -- shape of the points (any character)
3940 colour -- colour of the points
40- title -- title of the plot
41+ title -- title of the plot
4142 """
42-
43+
4344 if f :
4445 if isinstance (f , str ):
4546 f = open (f )
46-
47+
4748 data = [tuple (map (float , line .strip ().split (',' ))) for line in f ]
4849 xs = [i [0 ] for i in data ]
4950 ys = [i [1 ] for i in data ]
@@ -52,29 +53,29 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
5253 ys = [float (str (row ).strip ()) for row in open (ys )]
5354
5455 plotted = set ()
55-
56+
5657 if title :
57- print box_text (title , 2 * len (get_scale (xs , False , size ))+ 1 )
58-
59- print "-" * (2 * len (get_scale (xs , False , size ))+ 2 )
58+ print ( box_text (title , 2 * len (get_scale (xs , False , size )) + 1 ) )
59+
60+ print ( "-" * (2 * len (get_scale (xs , False , size )) + 2 ) )
6061 for y in get_scale (ys , True , size ):
61- print "|" ,
62+ print ( "|" , end = ' ' )
6263 for x in get_scale (xs , False , size ):
6364 point = " "
6465 for (i , (xp , yp )) in enumerate (zip (xs , ys )):
6566 if xp <= x and yp >= y and (xp , yp ) not in plotted :
6667 point = pch
67- #point = str(i)
68+ #point = str(i)
6869 plotted .add ((xp , yp ))
69- if x == 0 and y == 0 :
70+ if x == 0 and y == 0 :
7071 point = "o"
71- elif x == 0 :
72+ elif x == 0 :
7273 point = "|"
73- elif y == 0 :
74+ elif y == 0 :
7475 point = "-"
7576 printcolour (point , True , colour )
76- print "|"
77- print "-" * ( 2 * len (get_scale (xs , False , size ))+ 2 )
77+ print ( "|" )
78+ print ( "-" * ( 2 * len (get_scale (xs , False , size )) + 2 ) )
7879
7980
8081def main ():
@@ -85,9 +86,10 @@ def main():
8586 parser .add_option ('-t' , '--title' , help = 'title for the chart' , default = "" , dest = 't' )
8687 parser .add_option ('-x' , help = 'x coordinates' , default = None , dest = 'x' )
8788 parser .add_option ('-y' , help = 'y coordinates' , default = None , dest = 'y' )
88- parser .add_option ('-s' , '--size' ,help = 'y coordinates' , default = 20 , dest = 'size' , type = 'int' )
89- parser .add_option ('-p' , '--pch' ,help = 'shape of point' , default = "x" , dest = 'pch' )
90- parser .add_option ('-c' , '--colour' , help = 'colour of the plot (%s)' % colour_help , default = 'default' , dest = 'colour' )
89+ parser .add_option ('-s' , '--size' , help = 'y coordinates' , default = 20 , dest = 'size' , type = 'int' )
90+ parser .add_option ('-p' , '--pch' , help = 'shape of point' , default = "x" , dest = 'pch' )
91+ parser .add_option ('-c' , '--colour' , help = 'colour of the plot (%s)' %
92+ colour_help , default = 'default' , dest = 'colour' )
9193
9294 opts , args = parser .parse_args ()
9395
@@ -97,8 +99,8 @@ def main():
9799 if opts .f or (opts .x and opts .y ):
98100 plot_scatter (opts .f , opts .x , opts .y , opts .size , opts .pch , opts .colour , opts .t )
99101 else :
100- print "nothing to plot!"
102+ print ( "nothing to plot!" )
101103
102104
103- if __name__ == "__main__" :
105+ if __name__ == "__main__" :
104106 main ()
0 commit comments