gpt4 book ai didi

python - 日期时间中每 10 分钟绘制一次

转载 作者:行者123 更新时间:2023-12-02 08:47:22 25 4
gpt4 key购买 nike

我使用的“df”对于每个日期时间都有多行。我想每 10 分钟绘制一个具有相同日期时间的所有坐标的散点图。 df_data

中每 10 分钟就有一个每个位置的数据条目

如果我手动将时间输入到 t_list = [datetime(2017, 12, 23, 06, 00, 00), datetime(2017, 12, 23, 06, 10, 00), datetime( 2017, 12, 23, 06, 20, 00)]但我想将其替换为使用 df 中的日期的内容,以便我可以将其用于多个数据集。

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import numpy as np

df_data = pd.read_csv('C:\data.csv')
df_data['datetime'] = pd.to_datetime(df_data['TimeStamp'] )
df = df_data[(df_data['datetime']>= datetime(2017, 12, 23, 06,00, 00)) &
(df_data['datetime']< datetime(2017, 12, 23, 07, 00, 00))]

##want a time array for all of the datetimes in the df
t_list = [datetime(2017, 12, 23, 06, 00, 00), datetime(2017, 12, 23, 06, 10, 00),
datetime(2017, 12,
23, 06, 20, 00)]

for t in t_list:
t_end = t + timedelta(minutes = 10)
t_text = t.strftime("%d-%b-%Y (%H:%M)")

#boolean indexing with multiple conditions, you should wrap each single condition in brackets
df_t = df[(df['datetime']>=t) & (df['datetime']<t_end)]

#get data into variable
ws = df_t['Sp_mean']
lat = df_t['x']
lon = df_t['y']
col = 0.75

#calc min/max for setting scale on images
min_ws = df['Sp_mean'].min()
max_ws = df['Sp_mean'].max()

plt.figure(figsize=(15,10))
plt.scatter(lon, lat, c=ws,s=300, vmin=min_ws, vmax=max_ws)
plt.title('event' + t_text,fontweight = 'bold',fontsize=18)
plt.show()

我尝试了几种方法来尝试将 datetime 复制为可迭代列表,但没有给出我想要的结果,最新的如下:

date_arrray = np.arange(np.datetime64(df['datetime']))
df['timedelta'] = pd.to_timedelta(df['datetime'])

示例数据集

enter image description here

最佳答案

接缝你对pandas不熟悉。您应该检查 resample function
df_data 成为您的原始数据:

# make a DatetimeIndex and resample it to 10-Min interval
df_data.index = pd.to_datetime(df_data['TimeStamp'])
resampled_data = df_data.resample('10Min')

# loop it:
min_ws = df['Sp_mean'].min()
max_ws = df['Sp_mean'].max()
col = 0.75
for start_time, sampled_df in resampled_data:
ws = sampled_df['Sp_mean']
lat = sampled_df['x']
lon = sampled_df['y']
plt.figure(figsize=(15,10))
plt.scatter(lon, lat, c=ws,s=300, vmin=min_ws, vmax=max_ws)
plt.title('event' + start_time.strftime('%Y-%m-%d %H:%M:%S'),fontweight = 'bold',fontsize=18)
plt.show()

关于python - 日期时间中每 10 分钟绘制一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58452977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com