-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplot.py
58 lines (40 loc) · 1.75 KB
/
plot.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
def plot_steps(data):
steps = data['activities-log-steps']
x = [datetime.strptime(d['dateTime'], '%Y-%M-%d').strftime("%A") for d in steps]
y = [float(d['value']) for d in steps]
plt.bar(x,y)
plt.title('Steps last 7 days')
plt.show()
def plot_sleep(data):
sleep = data['sleep']
x = [datetime.strptime(d['dateOfSleep'], '%Y-%M-%d').strftime("%A") for d in sleep][::-1]
deep = [float(d['levels']['summary']['deep']['minutes'])/60.0 for d in sleep][::-1]
light = [float(d['levels']['summary']['light']['minutes'])/60.0 for d in sleep][::-1]
rem = [float(d['levels']['summary']['rem']['minutes'])/60.0 for d in sleep][::-1]
awake = [float(d['levels']['summary']['wake']['minutes'])/60.0 for d in sleep][::-1]
barWidth = 0.15
r1 = np.arange(len(deep))
r2 = [x + barWidth for x in r1]
r3 = [x + barWidth for x in r2]
r4 = [x + barWidth for x in r3]
# Make the plot
plt.bar(r1, awake, color='#ffa600', width=barWidth, edgecolor='white', label='Awake')
plt.bar(r2, rem, color='#ff6361', width=barWidth, edgecolor='white', label='REM')
plt.bar(r3, light, color='#bc5090', width=barWidth, edgecolor='white', label='Light')
plt.bar(r4, deep, color='#003f5c', width=barWidth, edgecolor='white', label='Deep')
# Add xticks on the middle of the group bars
plt.xlabel('Day', fontweight='bold')
plt.ylabel('Hours', fontweight='bold')
plt.xticks([r + barWidth for r in range(len(deep))], x)
# Create legend & Show graphic
plt.legend()
plt.show()
if __name__ == '__main__':
with open('fitbit_data.json') as f:
data = json.load(f)
#plot_steps(data)
plot_sleep(data)