forked from philippelt/netatmo-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphLast3Days
More file actions
executable file
·72 lines (55 loc) · 2.67 KB
/
Copy pathgraphLast3Days
File metadata and controls
executable file
·72 lines (55 loc) · 2.67 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
68
69
70
71
72
#!/usr/bin/python
# coding=utf-8
# 2013-01 : philippelt@users.sourceforge.net
# This is an example of graphing Temperature and Humidity from a module on the last 3 days
# The Matplotlib library is used and should be installed before running this sample program
import datetime, time
import lnetatmo
from matplotlib import pyplot as plt
from matplotlib import dates
from matplotlib.ticker import FormatStrFormatter
# Access to the sensors
auth = lnetatmo.ClientAuth()
dev = lnetatmo.DeviceList(auth)
# Time of information collection : 3*24hours windows to now
now = time.time()
start = now - 3 * 24 * 3600
# Get Temperature and Humidity with GETMEASURE web service (1 sample every 30min)
resp = dev.getMeasure( device_id='xxxx', # Replace with your values
module_id='xxxx', # " " " "
scale="30min",
mtype="Temperature,Humidity",
date_begin=start,
date_end=now)
# Extract the timestamp, temperature and humidity from the more complex response structure
result = [(int(k),v[0],v[1]) for k,v in resp['body'].items()]
# Sort samples by timestamps (Warning, they are NOT sorted by default)
result.sort()
# Split in 3 lists for use with Matplotlib (timestamp on x, temperature and humidity on two y axis)
xval, ytemp, yhum = zip(*result)
# Convert the x axis values from Netatmo timestamp to matplotlib timestamp...
xval = [dates.date2num(datetime.datetime.fromtimestamp(x)) for x in xval]
# Build the two curves graph (check Matplotlib documentation for details)
fig = plt.figure()
plt.xticks(rotation='vertical')
graph1 = fig.add_subplot(111)
graph1.plot(xval, ytemp, color='r', linewidth=3)
graph1.set_ylabel(u'Température', color='r')
graph1.set_ylim(0, 25)
graph1.yaxis.grid(color='gray', linestyle='dashed')
for t in graph1.get_yticklabels() : t.set_color('r')
graph1.yaxis.set_major_formatter(FormatStrFormatter(u'%2.0f °C'))
graph2 = graph1.twinx()
graph2.plot(xval, yhum, color='b', linewidth=3)
graph2.set_ylabel(u'Humidité',color='b')
graph2.set_ylim(50,100)
for t in graph2.get_yticklabels(): t.set_color('b')
graph2.yaxis.set_major_formatter(FormatStrFormatter(u'%2i %%'))
graph1.xaxis.set_major_locator(dates.HourLocator(interval=6))
graph1.xaxis.set_minor_locator(dates.HourLocator())
graph1.xaxis.set_major_formatter(dates.DateFormatter("%d-%Hh"))
graph1.xaxis.grid(color='gray')
graph1.set_xlabel(u'Jour et heure de la journée')
# X display the resulting graph (you could generate a PDF/PNG/... in place of display).
# The display provides a minimal interface that notably allows you to save your graph
plt.show()