-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature_plot.py
More file actions
48 lines (37 loc) · 1.71 KB
/
temperature_plot.py
File metadata and controls
48 lines (37 loc) · 1.71 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
# Import standard libraries
from pathlib import Path
import datetime
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.dates import DayLocator, HourLocator, DateFormatter
def create_plot_png():
"""
Creates a png graph based on a day's readings
"""
readings_date = (datetime.date.today() -
datetime.timedelta(days=1)).isoformat()
readings_path = Path.cwd() / 'Temperature_Logs' / (readings_date + ".csv")
def dateconv(s): return datetime.datetime.strptime(
s.decode("utf-8"), "%H:%M:%S")
data = np.genfromtxt(readings_path, delimiter=',', names=('time', 'temp'),
converters={'time': dateconv},
dtype=[('time', datetime.datetime), ('temp', float)], skip_footer=1)
fig, ax = plt.subplots()
ax.plot_date(data['time'], data['temp'], 'D-')
# this is superfluous, since the autoscaler should get it right, but
# use date2num and num2date to convert between dates and floats if
# you want; both date2num and num2date convert an instance or sequence
ax.set_xlim(data['time'][0], data['time'][-1])
# The hour locator takes the hour or sequence of hours you want to
# tick, not the base multiple
ax.xaxis.set_major_locator(HourLocator(range(0, 25, 3)))
ax.xaxis.set_minor_locator(HourLocator(range(0, 25, 1)))
ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
ax.fmt_xdata = DateFormatter('%H:%M:%S')
fig.autofmt_xdate()
plt.title("Temperature Graph for {date}".format(date=readings_date))
plt.xlabel('Time')
plt.ylabel('Temp(deg C)')
plt.savefig(Path.cwd() / 'Temperature_Graphs' /
"{date}.png".format(date=readings_date))