]> ToastFreeware Gitweb - toast/airingbutler.git/blob - plot_mhz19.py
annotate-output now accepts a datetime format, so let's use it
[toast/airingbutler.git] / plot_mhz19.py
1 #!/usr/bin/python3
2
3 import csv
4 import argparse
5 from datetime import datetime, date, timedelta
6 from matplotlib import pyplot as plt
7
8 '''
9 # annotate-output +"%F %T" ./log_mhz19.py -d /dev/arduino | tee -a log
10 filename = 'log.csv'
11 Time,TS,CO2,Temp
12 2024-09-22 01:08:15,279472,1578,24
13 2024-09-22 01:08:20,284518,1577,24
14 '''
15
16 def main():
17     parser = argparse.ArgumentParser('Plot log.csv.')
18     parser.add_argument('filename', nargs='?', default='log.csv', help='Name of csv file')
19     parser.add_argument('-t', '--temperature', action='store_true', help='Include temperature plot')
20     args = parser.parse_args()
21
22     with open(args.filename) as f:
23         reader = csv.reader(f)
24         headers = next(reader)
25
26         time = []
27         co2 = []
28         temp = []
29         for row in reader:
30             dt = datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
31             if len(time) > 0 and dt - time[-1] < timedelta(hours=-2): # DST switch!
32                 dt += timedelta(days=1)
33             time.append(dt)
34             co2.append(int(row[2]))
35             temp.append(int(row[3]))
36
37         plt.plot(time, co2, c = 'red')
38         plt.title('CO₂')
39         plt.xlabel('time')
40         plt.ylabel('ppm')
41         plt.tick_params(axis = 'both', which = 'major')
42         if args.temperature is True:
43             plt.gca().twinx()
44             plt.plot(time, temp, c = 'blue')
45         plt.show()
46
47 if __name__ == '__main__':
48     main()