Python分析睡眠数据,现在想要调整作息,分


在19年11月的时候买了一个运动手环,然后时不时会用它来记录睡眠数据;积累到现在已经有40个月了。现在想要调整作息,分析一下这些数据,来制定合理的作息计划。

 

图1 月平均入睡时间

从图1可以看出,我最经常的入睡时间是(02:00:00~02:10:00)之间;

现在我想要早睡,逐步调整,第一个目标值就是(00:50:00~01:00:00)之间,也就意味着我要在(00:20:00~00:30:00)停止刷手机【躺下到入睡需要30分钟】

 

 

 

 

 图2 月平均入睡时间 从图2可以看出,我经常在(08:00:00~08:10:00)以及(08:40:00~08:50:00)之间醒来。选(08:00:00~08:10:00)作为起床闹钟时间。

 

 

#coding:utf8

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use("TkAgg")
import time
import datetime


"""
目标:统计分析出月入睡时间的数据
大纲:

"""

"""
读取excel 
------------------------------
月份        入睡时间    起床时间
2019年11月 1:44:00    9:11:00
2019年12月 1:45:00    9:12:00
------------------------------
"""
df = pd.read_excel('./入睡时间月级.xls', sheet_name=0)

# 数据准备 按列获取数据作为x坐标轴
x = df['月份'].to_numpy()
y1 = np.array([])
for item in df['入睡时间'].to_numpy():
    y1 = np.append(y1,item.hour+(item.minute)/100)

y2 = np.array([])
for item in df['起床时间'].to_numpy():
    y2 = np.append(y2,item.hour+(item.minute)/100)

y3 = np.array([])
for item in (pd.to_timedelta(df['起床时间'].to_numpy().astype(str)) - pd.to_timedelta(df['入睡时间'].to_numpy().astype(str))):
    y3 = np.append(y3,int((item.seconds)/3600)+((((item.seconds)/60)%60)/100))


# 画图1 月平均入睡时间频数图
bins = [0.1,0.2,0.4,0.5,1.0,1.1,1.2,1.3,1.4,1.5,2.0,2.1,2.2,2.3,2.4,2.5,3.0,4.0,5.0,6.0]
segments = pd.cut(y2,bins,right=False)
counts = pd.value_counts(segments,sort=False)
b = plt.bar(counts.index.astype(str),counts)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.xticks(rotation=45)
plt.bar_label(b,counts)
plt.xlabel("月平均入睡时间")
plt.ylabel("")
plt.title("入睡时间频数图")
plt.show()


# 画2 月平均起床时间频数图
# bins = [7.4,7.5,8.0,8.1,8.2,8.3,8.4,8.5,9.0,9.1,9.2,9.3,9.4,9.5,10.0,10.1,10.2,10.3,11.0]
# segments = pd.cut(y2,bins,right=False)
# counts = pd.value_counts(segments,sort=False)
# b = plt.bar(counts.index.astype(str),counts)
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.xticks(rotation=45)
# plt.bar_label(b,counts)
# plt.xlabel("月平均起床时间")
# plt.ylabel("次")
# plt.title("起床时间频数图")
# plt.show()

# 画图3 直方图
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.bar(x,y2)
# plt.bar(x,y1)
# plt.plot(x,y3,'r')
# plt.xticks(rotation=60)
# plt.show()

评论关闭