python画ERA5数据的降水图代码
时间: 2023-09-14 07:09:23 浏览: 288
用Python下载ERA5数据超详细教程
以下是Python绘制ERA5降水数据图的示例代码:
```python
import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
# 读取ERA5数据
ds = xr.open_dataset('ERA5_precipitation.nc')
# 获取经纬度和时间坐标
lon = ds.longitude
lat = ds.latitude
time = ds.time
# 计算全球平均降水量
global_mean_precip = ds['tp'].mean(dim=['longitude', 'latitude'])
# 绘制全球平均降水量时间序列图
fig, ax = plt.subplots()
ax.plot(time, global_mean_precip)
ax.set_xlabel('Time')
ax.set_ylabel('Global Mean Precipitation (mm/day)')
ax.set_title('ERA5 Global Mean Precipitation')
plt.show()
# 选择时间和经度范围,并计算平均降水量
start_time = '2018-01-01'
end_time = '2018-12-31'
min_lon = 0
max_lon = 360
precip = ds['tp'].sel(time=slice(start_time, end_time), longitude=slice(min_lon, max_lon)).mean(dim='time')
# 绘制降水量地图
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
# 添加地图特征
ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
# 绘制降水量等值线图
levels = np.arange(0, 8, 1)
contour = ax.contourf(lon, lat, precip, cmap='Blues', levels=levels, transform=ccrs.PlateCarree())
plt.colorbar(contour, ax=ax)
# 设置地图范围和标题
ax.set_extent([min_lon, max_lon, -90, 90], crs=ccrs.PlateCarree())
ax.set_title('ERA5 Precipitation (2018)')
plt.show()
```
需要注意的是,需要提前安装xarray、matplotlib、cartopy等库。同时,需要替换代码中的ERA5数据文件路径。
阅读全文