gpt4 book ai didi

python - 如何在 python 中绘制时间序列上的一个点

转载 作者:行者123 更新时间:2023-12-05 05:49:43 27 4
gpt4 key购买 nike

我在时间序列中绘制点时遇到问题,因为我无法识别 y 轴值。我有 2 个数据集:一个包含卫星数据(海面温度)的 NetCDF 文件,另一个包含 Storm 轨迹数据(时间、经度、纬度、风速等)的 CSV 文件。我可以为位于海洋中的所有 Storm 路径位置绘制所需的温度时间序列。但是,我想在每个时间序列线中指示 Storm 足迹发生的时间。因此,一条线代表一个位置和随时间变化的温度,但我还想显示该位置何时发生 Storm 。

到目前为止,这是我的代码(有效):

lati = stormtrack_lat.values
loni = stormtrack_lon.values

for i, dummy in enumerate(lati):
dsloc = SSTskin_file.sel(lon=loni[i], lat=lati[i], method='nearest')
dsloc['analysed_sst'].plot()
#plt.plot(dsloc,stormtrack_time[i], "or") #here I want to add one point to each line indicating the time when the storm occured at this location
plt.title('My title')
plt.xlabel('time')
plt.ylabel('SST skin in K')

blah

netcdf 文件包含时间数据作为日期时间坐标,但我的 CSV 文件包含此数据:

|    da   | INDEX | SIZE  |  AREA     |   x     |  y |  RADIUS   |  MEANV

2020021018 1505 2934 177.363 -2.82 49.87 1782.18 16.18

2020021100 1505 3812 220.078 5.25 49.57 2811.51 16.17
...

其中“da”代表日期和时间 (YYYYMMDDHH)。所以,我想我需要 (1) 将 CSV 'da' 值转换为日期时间格式,以便将它们用于行 plt.plot(dsloc,stormtrack_time[i], "or") 然后 (2) 在 netcdf 文件中找到那些日期时间值,然后 (3) 使用比时间点绘制相应的 SST 值/时间点。

问题是我不知道该怎么做。谁能帮忙?

谢谢!

最佳答案

我找到了这样做的方法:

lati = stormtrack_lat.values
loni = stormtrack_lon.values
timei = stormtrack_datetime.values

fig2 = plt.figure(figsize=(20, 20), dpi=300)

for i, dummy in enumerate(lati):
dsloc = SSTskin_file.sel(lon=loni[i], lat=lati[i], method='nearest')
dstime = SSTskin_file.sel(time=timei[i], lon=loni[i], lat=lati[i], method='nearest')
skin_celsius = (dsloc['analysed_sst']) - 273.15
timesteps = dsloc.time.values
timestep = dstime.time.values
timevalue = ((dstime['analysed_sst']).values) - 273.15
lineplot = plt.plot(timesteps, skin_celsius )
dotplot = plt.plot(timestep, timevalue, "or")
plt.title('Skin SST over time at storm track locations', fontsize = 20 )
plt.xlabel('Date', fontsize = 16)
plt.ylabel('Skin SST in $^{\circ}C$', fontsize = 16)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
#plt.legend(lineplot) #Here I would like to plot the legend for the line plots (time series data). I want to indicate the location (longitude and latitude) of the time series
plt.legend(dotplot[:1], ['Storm track at location and time'], fontsize = 16);
fig2.savefig('SSTskin_storm_timeseries_test.png', bbox_inches='tight')

new plot

关于python - 如何在 python 中绘制时间序列上的一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70626139/

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