谢谢你对我的问题感兴趣
我希望绘制一张温度时间序列图,特别是1981-1999年1月至8月之间的温度时间序列图
以下是我的代码和尝试:
temperature = xr.open_dataarray('temperature.nc')
temp = temperature.sel(latitude=slice(34.5,30), longitude=slice(73,78.5))
templatlonmean = temp.mean(dim=['latitude','longitude'])-273.15
tempgraph1 = templatlonmean.sel(time=slice('1981','1999'))
上述命令读取良好,没有任何错误
以下是我将月份划分为季节的尝试:
第一次尝试
tempseason1 = tempgraph1.groupby("time.season").mean("time")
#Plotting Graph Command
myfig, myax = plt.subplots(figsize=(14,8))
timeyears = np.unique(tempgraph1["time.season"])
tempseason1.plot.line('b-', color='red', linestyle='--',linewidth=4, label='1981-1999 Mean')
我得到了这个错误: 绘图要求坐标为数字、布尔值或numpy.datetime64、datetime.datetime、cftime.datetime或pandas.Interval类型的日期。而不是接收对象类型的数据。
这是我的第二次尝试(从这篇文章Select xarray/pandas index based on specific months中检索) 但是,我不确定如何用这个来绘制图表,因此我尝试了以下方法:
def is_amj(month):
return (month >= 4) & (month <= 6)
temp_seasonal = tempgraph1.sel(time=is_amj(tempgraph1['time.month']))
#Plotting Graph Command
timeyears = np.unique(tempgraph1["time.season"])
temp_seasonal.plot.line('b-', color='red', linestyle='--',linewidth=4, label='1981-1999 Mean')
所以我继续我的第三次尝试(从这里开始http://xarray.pydata.org/en/stable/examples/monthly-means.html):
month_length = tempmean.time.dt.days_in_month
weights = month_length.groupby('time.season') / month_length.groupby('time.season').sum()
np.testing.assert_allclose(weights.groupby('time.season').sum().values, np.ones(4))
ds_weighted = (tempmean * weights).groupby('time.season').sum(dim='time')
ds_unweighted = tempmean.groupby('time.season').mean('time')
#Plot Commands
timeyears = np.unique(tempgraph1["time.season"])
ds_unweighted.plot.line('b-', color='red', linestyle='--',linewidth=4, label='1981-1999 Mean')
但我还是犯了与第一次尝试相同的错误: 打印要求坐标为数值、布尔值或numpy.datetime64、datetime.datetime、cftime.datetime或pandas.Interval类型的日期。而不是接收对象类型的数据
当我使用这个命令来绘制天气图而不是时间序列图时,我相信groupby过程会相似甚至相同,这就是我使用它的原因
然而,由于我在编码方面相对较新,请原谅任何语法错误,我无法找到任何明显的方法来实现这一点
因此,我想知道您是否可以建议其他方法为xarray绘制特定的月度数据,或者是否需要对我尝试的命令进行任何调整
我非常感谢你的慷慨帮助。 请让我知道,如果你需要更多的信息,我会尽快回复。 谢谢大家!
关于你的问题1。3.对象是分组的季节。
您可以通过执行以下操作将其可视化:
您应该看到如下内容:
注意季节维度的类型对象
我认为你应该在这里使用
resample
而不是groupby
。 重采样基本上是一个groupby向上采样或向下采样时间序列。 它看起来像:参数“Q”是季度频率的一个偏移量,参见那里的details
不过,我对策划知之甚少
相关问题 更多 >
编程相关推荐