- Notifications
You must be signed in to change notification settings - Fork142
Expand file tree
/
Copy pathscatterplot.py
More file actions
113 lines (91 loc) · 3.58 KB
/
scatterplot.py
File metadata and controls
113 lines (91 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Plotting terminal based scatterplots
"""
from __future__importprint_function
importcsv
importsys
importoptparse
from .utils.helpersimport*
from .utils.commandhelpimportscatter
defget_scale(series,is_y=False,steps=20):
min_val=min(series)
max_val=max(series)
scaled_series= []
forxindrange(min_val,max_val, (max_val-min_val)/steps,
include_stop=True):
ifx>0andscaled_seriesandmax(scaled_series)<0:
scaled_series.append(0.0)
scaled_series.append(x)
ifis_y:
scaled_series.reverse()
returnscaled_series
def_plot_scatter(xs,ys,size,pch,colour,title,cs):
plotted=set()
iftitle:
print(box_text(title,2*len(get_scale(xs,False,size))+1))
print("-"* (2*len(get_scale(xs,False,size))+2))
foryinget_scale(ys,True,size):
print("|",end=' ')
forxinget_scale(xs,False,size):
point=" "
for (i, (xp,yp))inenumerate(zip(xs,ys)):
ifxp<=xandyp>=yand (xp,yp)notinplotted:
point=pch
plotted.add((xp,yp))
ifcs:
colour=cs[i]
printcolour(point,True,colour)
print(" |")
print("-"* (2*len(get_scale(xs,False,size))+2))
defplot_scatter(f,xs,ys,size,pch,colour,title):
"""
Form a complex number.
Arguments:
f -- comma delimited file w/ x,y coordinates
xs -- if f not specified this is a file w/ x coordinates
ys -- if f not specified this is a filew / y coordinates
size -- size of the plot
pch -- shape of the points (any character)
colour -- colour of the points
title -- title of the plot
"""
cs=None
iff:
ifisinstance(f,str):
withopen(f)asfh:
data= [tuple(line.strip().split(','))forlineinfh]
else:
data= [tuple(line.strip().split(','))forlineinf]
xs= [float(i[0])foriindata]
ys= [float(i[1])foriindata]
iflen(data[0])>2:
cs= [i[2].strip()foriindata]
elifisinstance(xs,list)andisinstance(ys,list):
pass
else:
withopen(xs)asfh:
xs= [float(str(row).strip())forrowinfh]
withopen(ys)asfh:
ys= [float(str(row).strip())forrowinfh]
_plot_scatter(xs,ys,size,pch,colour,title,cs)
defmain():
parser=optparse.OptionParser(usage=scatter['usage'])
parser.add_option('-f','--file',help='a csv w/ x and y coordinates',default=None,dest='f')
parser.add_option('-t','--title',help='title for the chart',default="",dest='t')
parser.add_option('-x',help='x coordinates',default=None,dest='x')
parser.add_option('-y',help='y coordinates',default=None,dest='y')
parser.add_option('-s','--size',help='y coordinates',default=20,dest='size',type='int')
parser.add_option('-p','--pch',help='shape of point',default="x",dest='pch')
parser.add_option('-c','--colour',help='colour of the plot (%s)'%
colour_help,default='default',dest='colour')
opts,args=parser.parse_args()
ifopts.fisNoneand (opts.xisNoneoropts.yisNone):
opts.f=sys.stdin.readlines()
ifopts.for (opts.xandopts.y):
plot_scatter(opts.f,opts.x,opts.y,opts.size,opts.pch,opts.colour,opts.t)
else:
print("nothing to plot!")
if__name__=="__main__":
main()