-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
64 lines (52 loc) · 1.48 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
60
61
62
63
64
import sys
import matplotlib.pyplot as plt
if len(sys.argv) < 3:
print("Error: too few arguments!")
print(
"Usage: ./plot.py input_data_filename output_filename [plot_title] [y_axis_label1] [y_axis_label2]"
)
exit()
# Read command line arguments
datafile = sys.argv[1]
outfile = sys.argv[2]
plottitle = ""
ylabel1 = ""
ylabel2 = ""
if len(sys.argv) >= 4:
plottitle = sys.argv[3]
if len(sys.argv) >= 5:
ylabel1 = sys.argv[4]
ylabel2 = sys.argv[5]
# Read input data file
labels = []
data = []
with open(datafile) as f:
content = f.read().splitlines()
# First row is assumed to contain labels for each column
labels = [l.strip() for l in content[0].split("\t")]
# Use the number of labels to determine the number of data columns
data = [[] for _ in range(len(labels))]
for line in content[1:]:
i = 0
for num in line.split():
data[i].append(float(num))
i += 1
if ylabel1 == "" and len(labels) == 2:
ylabel1 = labels[1]
if ylabel2 == "" and len(labels) == 3:
ylabel2 = labels[2]
# Initialize plot
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
# Plot each data set, using first column as x-axis data
# for i in range(1, len(labels)):
ax1.plot(data[0], data[1])
ax2.plot(data[0], data[2])
# Plot's aesthetics
ax1.set_title(plottitle)
ax1.set_ylabel(ylabel1)
ax2.set_xlabel(labels[0])
ax2.set_ylabel(ylabel2)
plt.tight_layout()
# Save figure
fig.savefig(outfile, dpi=600)
plt.close("all")