-
Notifications
You must be signed in to change notification settings - Fork 0
/
daygraph.py
34 lines (27 loc) · 900 Bytes
/
daygraph.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
tempdata = []
dates = []
# Read the data from db
con = mdb.connectcon = ('DBHOST', 'DBUSER', 'DMPASS', 'DB');
cur = con.cursor()
cur.execute("SELECT datetime, temp FROM readings WHERE datetime BETWEEN DATE_SUB(NOW(), INTERVAL 1 DAY) AND NOW()")
for i in range(cur.rowcount):
row = cur.fetchone()
date = matplotlib.dates.date2num(row[0])
dates.append(date)
tempdata.append(row[1])
con.close()
fig, ax = plt.subplots(figsize=(6,5))
ax.plot_date(dates, tempdata, fmt='-')
ax.xaxis.set_major_formatter( DateFormatter('%H:%M'))
ax.set_ylabel('Temperature F')
for label in ax.get_xticklabels():
label.set_rotation(60)
plt.tight_layout()
plt.savefig('/var/www/html/daytemp.png')