-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
59 lines (53 loc) · 1.61 KB
/
plot.py
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
#!/usr/bin/python
import sys
import datetime
import matplotlib.pyplot as plt
plt.title('Convex Hull')
plt.grid(True)
no_of_args = len(sys.argv)
if(no_of_args < 2):
sys.stderr.write("ERROR!, Expected atleast one argument, output.txt file\n \
Usage - This program takes atleast one argument, The text file containing \
space separated coordinates in a line. You can also pass a input file containing \
list of all coordinates. You can also pass '-s' flag to directly save the generated plot.\n \
Example - demo.py output.txt input.txt(OPTIONAL) -s(OPTIONAL)\n")
sys.exit(1)
out_x = []
out_y = []
with open(sys.argv[1], "r") as f:
for line in f:
try:
a,b = map(float, line.split())
out_x.append(a)
out_y.append(b)
except:
sys.stderr.write("ERROR! - File is malformed. It should have two space separated \
real numbers per line.")
sys.exit(1)
f.close()
out_x += out_x
out_y += out_y
plt.scatter(out_x, out_y)
plt.plot(out_x, out_y,color='magenta')
if((no_of_args == 3 and sys.argv[2] != '-s') or (no_of_args > 3)):
inp_x = []
inp_y = []
with open(sys.argv[2], "r") as f:
lines_2_to_end = f.readlines()[1:]
for line in lines_2_to_end:
try:
a,b = map(float, line.split())
inp_x.append(a)
inp_y.append(b)
except:
sys.stderr.write("ERROR! - File is malformed. It should have two space separated \
real numbers per line., with first line containing no. of points.")
sys.exit(1)
f.close()
plt.scatter(inp_x, inp_y)
if(sys.argv[no_of_args-1] == '-s'):
filename = datetime.datetime.now()
filename = "plot-"+str(filename)+".jpg"
plt.savefig(filename)
else:
plt.show()