我在python中有一个问题,我希望你能帮助我解决它。我有一个数据列表,如下所示:
01/31/2021 08:51:41.222 5777 40.26
01/31/2021 08:51:41.441 5802 40.26
01/31/2021 08:51:41.644 5786 40.26
01/31/2021 08:51:41.847 5793 40.29
01/31/2021 08:51:42.050 5776 40.29
01/31/2021 08:51:42.269 5798 40.29
01/31/2021 08:51:42.456 5791 40.26
01/31/2021 08:51:42.660 5800 40.26
01/31/2021 08:51:42.878 5781 40.26
01/31/2021 08:51:43.081 5793 40.29
01/31/2021 08:51:43.300 5795 40.29
01/31/2021 08:51:43.519 5799 40.29
我的目标是在选定的时间范围内保存数据并绘制这些数据。这就是我现在编码的内容:
import numpy as np
import matplotlib.pyplot as plt
import datetime as dt
import os
os.chdir('C:\\Users\\Nic\\Desktop')
filename = "20"
with open(filename + ".txt") as file:
lines = file.readlines()[10:]
date = [line.split()[0] for line in lines]
hours =[line.split()[1] for line in lines]
rad = [float(line.split()[2]) for line in lines]
starthour = "0:00"
endhour = "00:51"
date_str = [a + " " + b for a, b in zip(date, hours)]
data_obj = [dt.datetime.strptime(d, "%m/%d/%Y %H:%M:%S.%f") for d in date_str]
date_start = dt.datetime.strptime(date[0] + " " + starthour, "%m/%d/%Y %H:%M")
date_end = dt.datetime.strptime(date[0] + " " + endhour, "%m/%d/%Y %H:%M")
rad = [r for r, d in zip(rad, data_obj) if (d>=date_start) & (d<=date_end)]
data_obj = [d for d in data_obj if (d>=date_start) & (d<=date_end)]
plt.plot(data_obj, rad, color = "turquoise", label = "dati", linewidth = "0.5")
plt.show()
plt.savefig(filename + ".png", dpi=300)
现在我有另一个问题。我想分析这些数据,即 50 分钟的数据并将其划分为子间隔,例如每 5 分钟一共有 10 个子间隔。我对原始文件中接近小时的列“rad”数据感兴趣。我想要做的是获得每个子区间的平均值(使用均值函数),添加标准偏差误差并将其绘制成图表。我有很多文件要以这种方式分析,我想获得一个自动化程序。
所有帮助或外部文档将不胜感激。我是python的新手,谢谢你给我的所有帮助。