5
5
Plotting terminal based scatterplots
6
6
"""
7
7
8
+ from __future__ import print_function
8
9
import csv
9
10
import sys
10
11
import optparse
11
12
from .utils .helpers import *
12
- from .utils .commandhelp import scatter
13
+ from .utils .commandhelp import scatter
13
14
14
15
15
16
def get_scale (series , is_y = False , steps = 20 ):
16
17
min_val = min (series )
17
18
max_val = max (series )
18
19
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 ):
20
21
if x > 0 and scaled_series and max (scaled_series ) < 0 :
21
22
scaled_series .append (0.0 )
22
23
scaled_series .append (x )
23
-
24
+
24
25
if is_y :
25
26
scaled_series .reverse ()
26
27
return scaled_series
@@ -29,21 +30,21 @@ def get_scale(series, is_y=False, steps=20):
29
30
def plot_scatter (f , xs , ys , size , pch , colour , title ):
30
31
"""
31
32
Form a complex number.
32
-
33
+
33
34
Arguments:
34
35
f -- comma delimited file w/ x,y coordinates
35
36
xs -- if f not specified this is a file w/ x coordinates
36
37
ys -- if f not specified this is a filew / y coordinates
37
38
size -- size of the plot
38
39
pch -- shape of the points (any character)
39
40
colour -- colour of the points
40
- title -- title of the plot
41
+ title -- title of the plot
41
42
"""
42
-
43
+
43
44
if f :
44
45
if isinstance (f , str ):
45
46
f = open (f )
46
-
47
+
47
48
data = [tuple (map (float , line .strip ().split (',' ))) for line in f ]
48
49
xs = [i [0 ] for i in data ]
49
50
ys = [i [1 ] for i in data ]
@@ -52,29 +53,29 @@ def plot_scatter(f, xs, ys, size, pch, colour, title):
52
53
ys = [float (str (row ).strip ()) for row in open (ys )]
53
54
54
55
plotted = set ()
55
-
56
+
56
57
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 ) )
60
61
for y in get_scale (ys , True , size ):
61
- print "|" ,
62
+ print ( "|" , end = ' ' )
62
63
for x in get_scale (xs , False , size ):
63
64
point = " "
64
65
for (i , (xp , yp )) in enumerate (zip (xs , ys )):
65
66
if xp <= x and yp >= y and (xp , yp ) not in plotted :
66
67
point = pch
67
- #point = str(i)
68
+ #point = str(i)
68
69
plotted .add ((xp , yp ))
69
- if x == 0 and y == 0 :
70
+ if x == 0 and y == 0 :
70
71
point = "o"
71
- elif x == 0 :
72
+ elif x == 0 :
72
73
point = "|"
73
- elif y == 0 :
74
+ elif y == 0 :
74
75
point = "-"
75
76
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 ) )
78
79
79
80
80
81
def main ():
@@ -85,9 +86,10 @@ def main():
85
86
parser .add_option ('-t' , '--title' , help = 'title for the chart' , default = "" , dest = 't' )
86
87
parser .add_option ('-x' , help = 'x coordinates' , default = None , dest = 'x' )
87
88
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' )
91
93
92
94
opts , args = parser .parse_args ()
93
95
@@ -97,8 +99,8 @@ def main():
97
99
if opts .f or (opts .x and opts .y ):
98
100
plot_scatter (opts .f , opts .x , opts .y , opts .size , opts .pch , opts .colour , opts .t )
99
101
else :
100
- print "nothing to plot!"
102
+ print ( "nothing to plot!" )
101
103
102
104
103
- if __name__ == "__main__" :
105
+ if __name__ == "__main__" :
104
106
main ()
0 commit comments