import xarray as xr import matplotlib.pyplot as plt if __name__=='__main__': ds=xr.open_dataset(r'D:\pythonsx\zl\sst.mnmean.nc') sst=ds['sst'] sstsel=sst.loc[:,5:-5,190:240] #5°S-5°N,170°W-120°W enso=sstsel.groupby('time.year').mean(dim=['lat', 'lon'])修改这段代码,求每年平均
时间: 2023-06-13 16:07:12 浏览: 298
修改后的代码如下:
```python
import xarray as xr
import matplotlib.pyplot as plt
if __name__=='__main__':
ds = xr.open_dataset(r'D:\pythonsx\zl\sst.mnmean.nc')
sst = ds['sst']
sstsel = sst.sel(lat=slice(5,-5), lon=slice(360-170, 360-120)) # 5°S-5°N,170°W-120°W
enso = sstsel.groupby('time.year').mean(dim=['lat', 'lon'])
print(enso)
enso.plot()
plt.show()
```
主要修改的是:
- `sstsel` 的选取方式,使用 `sel` 函数选择经纬度范围;
- `groupby` 函数中的 `dim` 参数,由原来的 `['lat', 'lon']` 改为 `['time']`,求每年平均的时候只需要在时间维度上进行平均。
相关问题
import xarray as xr import matplotlib.pyplot as plt if __name__=='__main__': ds=xr.open_dataset(r'D:\pythonsx\zl\sst.mnmean.nc') sst=ds['sst'] sstsel=sst.loc[:,5:-5,190:240] #5°S-5°N,170°W-120°W enso=sstsel.mean(dim=['lat', 'lon'])#区域平均修改这段代码,计算enso的距平
import xarray as xr
import matplotlib.pyplot as plt
if __name__=='__main__':
ds=xr.open_dataset(r'D:\pythonsx\zl\sst.mnmean.nc')
sst=ds['sst']
sstsel=sst.loc[:,5:-5,190:240] #5°S-5°N,170°W-120°W
enso_mean=sstsel.mean(dim=['lat', 'lon']) #区域平均
enso_anom= enso_mean - enso_mean.mean() #计算enso的距平
import xarray as xr import matplotlib.pyplot as plt if name=='main': ds=xr.open_dataset(r'D:\pythonsx\zl\sst.mnmean.nc') sst=ds['sst'] sstsel=sst.loc[:,5:-5,190:240] #5°S-5°N,170°W-120°W enso=sstsel.mean(dim=['lat', 'lon'])#区域平均 ensom=enso.mean('time') #print(ensom) ensoa=enso-ensom #print(ensoa)修改这段代码,筛选出大于0.5的值
import xarray as xr
import matplotlib.pyplot as plt
if __name__ == '__main__':
ds = xr.open_dataset(r'D:\pythonsx\zl\sst.mnmean.nc')
sst = ds['sst']
sstsel = sst.loc[:, 5:-5, 190:240] # 5°S-5°N,170°W-120°W
enso = sstsel.mean(dim=['lat', 'lon']) # 区域平均
ensom = enso.mean('time')
ensoa = enso - ensom
# 选取大于0.5的值
ensoa_selected = ensoa.where(ensoa > 0.5, drop=True)
print(ensoa_selected)
阅读全文