翼度科技»论坛 编程开发 python 查看内容

Python分析睡眠数据

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
在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:40:00~08:50:00)作为起床闹钟时间。
 
 图3 各月睡眠时间条形图
  1. #coding:utf8
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib as mpl
  5. import matplotlib.pyplot as plt
  6. mpl.use("TkAgg")
  7. import time
  8. import datetime
  9. """
  10. 目标:统计分析出月入睡时间的数据
  11. 大纲:
  12. """
  13. """
  14. 读取excel
  15. ------------------------------
  16. 月份        入睡时间    起床时间
  17. 2019年11月 1:44:00    9:11:00
  18. 2019年12月 1:45:00    9:12:00
  19. ------------------------------
  20. """
  21. df = pd.read_excel('./入睡时间月级.xls', sheet_name=0)
  22. # 数据准备 按列获取数据作为x坐标轴
  23. x = df['月份'].to_numpy()
  24. y1 = np.array([])
  25. for item in df['入睡时间'].to_numpy():
  26.     y1 = np.append(y1,item.hour+(item.minute)/100)
  27. y2 = np.array([])
  28. for item in df['起床时间'].to_numpy():
  29.     y2 = np.append(y2,item.hour+(item.minute)/100)
  30. y3 = np.array([])
  31. for item in (pd.to_timedelta(df['起床时间'].to_numpy().astype(str)) - pd.to_timedelta(df['入睡时间'].to_numpy().astype(str))):
  32.     y3 = np.append(y3,(item.seconds)/3600)
  33. # 画图1 月平均入睡时间频数图
  34. 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]
  35. segments = pd.cut(y2,bins,right=False)
  36. counts = pd.value_counts(segments,sort=False)
  37. b = plt.bar(counts.index.astype(str),counts)
  38. plt.rcParams['font.sans-serif'] = ['SimHei']
  39. plt.xticks(rotation=45)
  40. plt.bar_label(b,counts)
  41. plt.xlabel("月平均入睡时间")
  42. plt.ylabel("次")
  43. plt.title("入睡时间频数图")
  44. plt.show()
  45. # 画2 月平均起床时间频数图
  46. # 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]
  47. # segments = pd.cut(y2,bins,right=False)
  48. # counts = pd.value_counts(segments,sort=False)
  49. # b = plt.bar(counts.index.astype(str),counts)
  50. # plt.rcParams['font.sans-serif'] = ['SimHei']
  51. # plt.xticks(rotation=45)
  52. # plt.bar_label(b,counts)
  53. # plt.xlabel("月平均起床时间")
  54. # plt.ylabel("次")
  55. # plt.title("起床时间频数图")
  56. # plt.show()
  57. # 画图3 直方图
  58. # plt.rcParams['font.sans-serif'] = ['SimHei']
  59. # plt.bar(x,y2)
  60. # plt.bar(x,y1)
  61. # plt.plot(x,y3,'r')
  62. # plt.xticks(rotation=60)
  63. # plt.show()
复制代码
 

来源:https://www.cnblogs.com/Mozhiqin/p/17107455.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具