在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 各月睡眠時間條形圖
#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,(item.seconds)/3600) # 畫圖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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/543390.html
標籤:Python
上一篇:Python裝飾器實體講解(二)
