为长辈所制作,希望大家都能珍惜眼前人,且行且珍惜~

血压走势图python小试

代码模块:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime,date
import matplotlib.dates as mdates
import matplotlib


data=pd.read_excel('血压记录.xlsx')

############## 读取表格数据 ##############
datee=data['日期']

mor_high=data['早高']
mor_low=data['早低']
mor_pul=data['早脉搏']

noon_high=data['中高']
noon_low=data['中低']
noon_pul=data['中脉搏']

night_high=data['晚高']
night_low=data['晚低']
night_pul=data['晚脉搏']


############## 画图 ##############
matplotlib.rcParams['font.family'] = 'FangSong'
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'


nan_index=np.where(np.isnan(mor_high))[0]
new_mor_high=np.delete(np.array(mor_high),np.array(list(nan_index)))
new_mor_low=np.delete(np.array(mor_low),np.array(list(nan_index)))
new_mor_pul=np.delete(np.array(mor_pul),np.array(list(nan_index)))
new_mor_date=np.delete(np.array(datee),np.array(list(nan_index)))
new_mor_date= [datetime.strptime(d, '%m/%d').date() for d in new_mor_date]

nan_index=np.where(np.isnan(noon_high))[0]
new_noon_high=np.delete(np.array(noon_high),np.array(list(nan_index)))
new_noon_low=np.delete(np.array(noon_low),np.array(list(nan_index)))
new_noon_pul=np.delete(np.array(noon_pul),np.array(list(nan_index)))
new_noon_date=np.delete(np.array(datee),np.array(list(nan_index)))
new_noon_date= [datetime.strptime(d, '%m/%d').date() for d in new_noon_date]

nan_index=np.where(np.isnan(night_high))[0]
new_night_high=np.delete(np.array(night_high),np.array(list(nan_index)))
new_night_low=np.delete(np.array(night_low),np.array(list(nan_index)))
new_night_pul=np.delete(np.array(night_pul),np.array(list(nan_index)))
new_night_date=np.delete(np.array(datee),np.array(list(nan_index)))
new_night_date= [datetime.strptime(d, '%m/%d').date() for d in new_night_date]

fig=plt.figure(figsize=(13,36))

##### 早 #####
ax=fig.add_subplot(411)
lns1=ax.plot(new_mor_date,new_mor_high,'bD--',alpha=0.7,label='高压')
lns2=ax.plot(new_mor_date,new_mor_low,'rD--',alpha=0.7,label='低压')
ax.set_xlabel('日期',fontsize=20)
ax.set_ylabel('毫米汞柱',fontsize=20)
ax2=ax.twinx()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
ax.tick_params(labelsize=14)
lns3=ax2.plot(new_mor_date,new_mor_pul,'g>--',alpha=0.5,label='脉搏数')
ax2.set_ylabel('脉搏数/分钟',fontsize=20)
lns=lns1+lns2+lns3
labs=[l.get_label() for l in lns]
ax.legend(lns, labs,loc='upper left',fontsize=14)
ax.grid(linestyle='--')
plt.title('早晨血压情况(8:00 - 11:00)',fontsize=20)
ax2.tick_params(labelsize=14)

##### 中 #####
ax=fig.add_subplot(412)
lns1=ax.plot(new_noon_date,new_noon_high,'bD--',alpha=0.7,label='高压')
lns2=ax.plot(new_noon_date,new_noon_low,'rD--',alpha=0.7,label='低压')
ax.set_xlabel('日期',fontsize=20)
ax.set_ylabel('毫米汞柱',fontsize=20)
ax2=ax.twinx()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
ax.tick_params(labelsize=14)
lns3=ax2.plot(new_noon_date,new_noon_pul,'g>--',alpha=0.5,label='脉搏数')
ax2.set_ylabel('脉搏数/分钟',fontsize=20)
lns=lns1+lns2+lns3
labs=[l.get_label() for l in lns]
ax.legend(lns, labs,loc='upper left',fontsize=14)
ax.grid(linestyle='--')
plt.title('中午血压情况(11:00 - 20:00)',fontsize=20)
ax2.tick_params(labelsize=14)

##### 晚 #####
ax=fig.add_subplot(413)
lns1=ax.plot(new_night_date,new_night_high,'bD--',alpha=0.7,label='高压')
lns2=ax.plot(new_night_date,new_night_low,'rD--',alpha=0.7,label='低压')
ax.set_xlabel('日期',fontsize=20)
ax.set_ylabel('毫米汞柱',fontsize=20)
ax2=ax.twinx()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d'))
ax.tick_params(labelsize=14)
lns3=ax2.plot(new_night_date,new_night_pul,'g>--',alpha=0.5,label='脉搏数')
ax2.set_ylabel('脉搏数/分钟',fontsize=20)
lns=lns1+lns2+lns3
labs=[l.get_label() for l in lns]
ax.legend(lns, labs,loc='upper left',fontsize=14)
ax.grid(linestyle='--')
plt.title('晚上血压情况(20:00 - 次日)',fontsize=20)
ax2.tick_params(labelsize=14)

##### 平均 #####
	##### 求取一天内的平均值 #####
		##### 格式化 #####
datee=np.array(datee)
mor_high=np.array(mor_high)
mor_low=np.array(mor_low)
mor_pul=np.array(mor_pul)
noon_high=np.array(noon_high)
noon_low=np.array(noon_low)
noon_pul=np.array(noon_pul)
night_high=np.array(night_high)
night_low=np.array(night_low)
night_pul=np.array(night_pul)

avr_high=[]
avr_low=[]
avr_pul=[]
fac=3
for i in range(len(datee)):
	if np.isnan(mor_high[i]):
		fac=fac-1
		mor_high[i]=0.
	if np.isnan(noon_high[i]):
		fac=fac-1
		noon_high[i]=0.
	if np.isnan(night_high[i]):
		fac=fac-1
		night_high[i]=0.
	avr_high=np.append(avr_high,(mor_high[i]+noon_high[i]+night_high[i])/fac)
	fac=3
	if np.isnan(mor_low[i]):
		fac=fac-1
		mor_low[i]=0.
	if np.isnan(noon_low[i]):
		fac=fac-1
		noon_low[i]=0.
	if np.isnan(night_low[i]):
		fac=fac-1
		night_low[i]=0.
	avr_low=np.append(avr_low,(mor_low[i]+noon_low[i]+night_low[i])/fac)
	fac=3
	if np.isnan(mor_pul[i]):
		fac=fac-1
		mor_pul[i]=0.
	if np.isnan(noon_pul[i]):
		fac=fac-1
		noon_pul[i]=0.
	if np.isnan(night_pul[i]):
		fac=fac-1
		night_pul[i]=0.
	avr_pul=np.append(avr_pul,(mor_pul[i]+noon_pul[i]+night_pul[i])/fac)
	fac=3

ax=fig.add_subplot(414)
lns1=ax.plot(datee,avr_high,'bD--',alpha=0.7,label='高压')
lns2=ax.plot(datee,avr_low,'rD--',alpha=0.7,label='低压')
ax.set_xlabel('日期',fontsize=20)
ax.set_ylabel('毫米汞柱',fontsize=20)
ax2=ax.twinx()
ax.tick_params(axis='y',labelsize=14)
ax.tick_params(axis='x',labelsize=14,rotation=310)
lns3=ax2.plot(datee,avr_pul,'g>--',alpha=0.5,label='脉搏数')
ax2.set_ylabel('脉搏数/分钟',fontsize=20)
lns=lns1+lns2+lns3
labs=[l.get_label() for l in lns]
ax.legend(lns, labs,loc='upper left',fontsize=14)
ax.grid(linestyle='--')
plt.title('日平均水平',fontsize=20)
ax2.tick_params(labelsize=14)

#########  导出结果为一根据今日时间所命名的图片 
today=date.today()
s='{name}.jpg'
path=s.format(name=str(today))
plt.savefig(str(path),facecolor='#CDCDCD')

运行结果: