-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscatter-plot-by-date.py
executable file
·93 lines (71 loc) · 2.42 KB
/
scatter-plot-by-date.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
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
#!/usr/bin/env python3
import sys, csv
import matplotlib.pyplot as plt
import matplotlib as mpl
import datetime as dt
version = '1.0'
license_info = f"""scatter-plot-by-date v{version}: creates a scatter plot from a csv data file.
Copyright (C) 2018 John Gabriele
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
help_msg = """Usage:
scatter-plot-by-date.py 'My Plot Title' input.csv
scatter-plot-by-date.py --license
scatter-plot-by-date.py --version
This program comes with ABSOLUTELY NO WARRANTY. This is free
software, and you are welcome to redistribute it under certain
conditions; run it with the `--license` option for details.
Exiting.
"""
def main():
handle_args()
plot_title = sys.argv[1]
csv_fnm = sys.argv[2]
rows = read_csv(csv_fnm)
x_axis_label = rows[0][0]
y_axis_label = rows[0][1]
rows = rows[1:]
rows.sort(key=lambda r: r[0])
xs = [dt.datetime.strptime(row[0], '%Y-%m-%d') for row in rows]
ys = [float(row[1]) for row in rows]
fig, ax = plt.subplots()
ax.plot(xs, ys, color='tab:blue', marker="o")
ax.set(xlabel=x_axis_label, ylabel=y_axis_label, title=plot_title)
ax.grid()
fig.autofmt_xdate()
plt.show()
def handle_args():
if len(sys.argv) == 1:
print(help_msg)
sys.exit()
elif len(sys.argv) == 2:
if sys.argv[1] == '--license':
print(license_info)
sys.exit()
elif sys.argv[1] == '--version':
print(f"scatter-plot-by-date, v{version}")
sys.exit()
else:
print(help_msg)
sys.exit()
elif len(sys.argv) != 3:
print(help_msg)
sys.exit()
def read_csv(fnm):
rows = []
with open(fnm, newline='') as f:
reader = csv.reader(f)
for row in reader:
rows.append(row)
return rows
#=================================
main()