fix datetime in plot
[toast/airingbutler.git] / plot_mhz19.py
1 #!/usr/bin/python3
2
3 import csv
4 from datetime import datetime, date, timedelta
5 from matplotlib import pyplot as plt
6
7 filename = 'log.csv'
8 '''
9 Time,TS,CO2,Temp
10 21:02:02,0,661,24
11 21:02:08,5021,657,24
12
13 '''
14
15 def parsedatetime(timevalue, currentdate):
16     dt = datetime.strptime(timevalue, '%H:%M:%S')
17     return datetime.combine(currentdate, dt.time())
18
19 with open(filename) as f:
20     reader = csv.reader(f)
21     headers = next(reader)
22
23     time = []
24     co2 = []
25     startdate = date(2020, 10, 22)
26     for row in reader:
27         dt = parsedatetime(row[0], startdate)
28         if len(time) > 0 and dt - time[-1] < timedelta(hours=-2): # DST switch!
29             startdate += timedelta(days=1)
30             dt += timedelta(days=1)
31         time.append(dt)
32         co2.append(int(row[2]))
33
34     plt.plot(time, co2, c = 'red')
35     plt.title('CO₂')
36     plt.xlabel('time')
37     plt.ylabel('ppm')
38     plt.tick_params(axis = 'both', which = 'major')
39     plt.show()