我想从包含多天数据(从2018年5月1日到2018年5月18日)的数据帧中保存6h块的绘图。你知道吗
我的数据帧“EperDtPanda”有以下格式:
ldr
timestamp
2018-05-01T00:00:03.454+02:00 972
2018-05-01T00:00:08.532+02:00 972
2018-05-01T00:00:13.462+02:00 973
2018-05-01T00:00:18.467+02:00 973
2018-05-01T00:00:23.472+02:00 968
2018-05-01T00:00:28.480+02:00 972
2018-05-01T00:00:33.487+02:00 973
2018-05-01T00:00:38.484+02:00 970
我的索引类型为:“timestamp”
我正在绘制整个数据周期,使用以下代码:
indicies = map(lambda t: np.datetime64(t), EperEtPanda.index)
newIndextValues = map(lambda v: v[0], EperEtPanda.values)
ts = pd.Series(newIndextValues, index=indicies)
series2 = ts.resample('H').mean()
plt.plot(series2.index, series2.values)
plt.xticks(rotation='vertical');
现在我想把这个图切成6h的图,然后保存这些图。 下面是我用来将图形分割为6h块的代码:
startDate = '2018-05-01T00:00:00+02:00'
endDate = '2018-05-18T00:00:00+02:00'
blockLength = 6
i = 0
while (str_to_ts(startDate) < str_to_ts(endDate)):
mask = (EperEtPanda.index >= str_to_ts(startDate)) & (EperEtPanda.index <= (str_to_ts(startDate) + timedelta(hours=blockLength)))
EperDtPanda6h = EperDtPanda.loc[mask]
slice6h = EperDtPanda6h.plot()
slice6h.get_figure().savefig('figure6h' + i + '.png')
startDate = str_to_ts(startDate) + timedelta(hours=blockLength)
i += 1
str \u to \u ts是一个函数,可将stings转换为时间戳:
str_to_ts = udf (lambda d: datetime.strptime(d, "%Y-%m-%dT%H:%M:%S.%f+02:00"), TimestampType())
但它不起作用。。你知道吗
有人知道怎么做吗?你知道吗
我想你可以做到:
希望对你有用
相关问题 更多 >
编程相关推荐