Python xarray KeyError: 'latitude' - 解决ERA5数据纬度排序问题
对不起,看起来ERA5数据集中没有名为'latitude'的坐标。为了正确选择逆序的纬度值,我们可以尝试使用[::-1]切片来逆序处理纬度数据。以下是修正后的代码:
import xarray as xr
import geopandas as gpd
import matplotlib.pyplot as plt
# 加载ERA5数据
era5_dataset = xr.open_dataset(r"I:\ERA5\era5.nc")
tp_average = era5_dataset["tp_average"]
tp_frequency = era5_dataset["tp_frequency"]
tp_intensity = era5_dataset["tp_intensity"]
lat_era5 = era5_dataset["latitude"][::-1] # 逆序处理纬度
# 创建Figure和Axes对象
fig, axes = plt.subplots(1, 3, figsize=(12, 5), sharey=True)
# 绘制tp_average变量的纬度变化曲线
axes[0].plot(lat_era5, tp_average)
axes[0].set_xlabel("Latitude", fontsize=12)
axes[0].set_ylabel("tp_average", fontsize=12)
axes[0].set_title("", fontsize=12)
# 绘制tp_frequency变量的纬度变化曲线
axes[1].plot(lat_era5, tp_frequency)
axes[1].set_xlabel("Latitude", fontsize=12)
axes[1].set_ylabel("tp_frequency", fontsize=12)
axes[1].set_title("", fontsize=12)
# 绘制tp_intensity变量的纬度变化曲线
axes[2].plot(lat_era5, tp_intensity)
axes[2].set_xlabel("Latitude", fontsize=12)
axes[2].set_ylabel("tp_intensity", fontsize=12)
axes[2].set_title("", fontsize=12)
plt.tight_layout()
plt.show()
在这个版本中,我使用[::-1]切片来逆序处理ERA5数据的纬度,以确保纬度的顺序正确。请注意,这里假设ERA5数据的纬度坐标名称为"latitude",你可能需要根据实际数据的特点进行适当的调整。
原文地址: https://www.cveoy.top/t/topic/byEF 著作权归作者所有。请勿转载和采集!