This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
67 lines (52 loc) · 1.76 KB
/
plot.py
File metadata and controls
67 lines (52 loc) · 1.76 KB
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
#!/usr/bin/python3
import sqlite3
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime, timedelta
from dateutil import tz
from matplotlib.dates import DateFormatter
import os
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
conn = sqlite3.connect('coffee.db', detect_types=sqlite3.PARSE_DECLTYPES)
c = conn.cursor()
# status: "unreachable", "no coffeepot", "ok"
STATUS = {
"ok": 0,
"no coffeepot": 1,
"unreachable": 2
}
def plot_it(png_name, titlestr, start_datetime = None):
c.execute("SELECT * from coffeepot;")
t = []
x = []
r = []
for t_, s, remaining_cups, raw in c.fetchall():
if start_datetime is not None and t_ < start_datetime:
continue
t += [t_]
x += [remaining_cups]
r += [raw]
# Data for plotting
fig, ax = plt.subplots()
ax.plot(t, x)
ax.set(xlabel='time', ylabel='Cups',
title='Remaining Cups in Coffee Pot' + titlestr, ylim=(-0.5,8.5))
ax.grid()
ax.set_yticks(range(0,9))
ax.legend(('Cups',), loc=2)
ax.xaxis.set_major_formatter(DateFormatter("%H:%M", tz=tz.gettz('Europe/Berlin')))
ax2 = ax.twinx()
ax2.plot(t, r, 'r')
ax2.set(ylim=(0, 1024), ylabel="Raw ADC Value")
ax2.legend(('Raw',), loc=1)
ax2.xaxis.set_major_formatter(DateFormatter("%H:%M", tz=tz.gettz('Europe/Berlin')))
fig.autofmt_xdate()
fig.savefig(png_name)
plot_it('plot.png', '')
plot_it('plot_1h.png', ' (last 1h)', datetime.now() - timedelta(hours = 1))
plot_it('plot_6h.png', ' (last 6h)', datetime.now() - timedelta(hours = 6))
plot_it('plot_24h.png', ' (last 24h)', datetime.now() - timedelta(hours = 24))