import xarray as xr
import geopandas as gpd
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

# 加载bcc数据
dataset = xr.open_dataset('I:\CMIP6HR\highresSST-present\bcc.nc')
pr_mean = dataset['pr_mean']
pr_frequency = dataset['pr_frequency']
pr_intensity = dataset['pr_intensity']

# 加载ERA5数据
era5_dataset = xr.open_dataset('I:\ERA5\era5.nc')
tp_average = era5_dataset['tp_average']
tp_frequency = era5_dataset['tp_frequency']
tp_intensity = era5_dataset['tp_intensity']

# 加载shp文件
shapefile = gpd.read_file('I:\MekongRiverBasin_whole\MekongRiverBasin.shp')
lat_min, lat_max = shapefile.bounds['miny'].min(), shapefile.bounds['maxy'].max()
lon_min, lon_max = shapefile.bounds['minx'].min(), shapefile.bounds['maxx'].max()

# 筛选出shpfile内部的经纬度范围
pr_mean = pr_mean.sel(lat=slice(lat_min, lat_max), lon=slice(lon_min, lon_max))
pr_frequency = pr_frequency.sel(lat=slice(lat_min, lat_max), lon=slice(lon_min, lon_max))
pr_intensity = pr_intensity.sel(lat=slice(lat_min, lat_max), lon=slice(lon_min, lon_max))

# 计算每一行格点的平均值
pr_mean_avg = pr_mean.mean(dim='lon')
pr_frequency_avg = pr_frequency.mean(dim='lon')
pr_intensity_avg = pr_intensity.mean(dim='lon')

# 创建Figure和Axes对象
fig, axes = plt.subplots(1, 3, figsize=(12, 5), sharey=False)

# 绘制pr_mean变量的纬度变化曲线
axes[0].plot(pr_mean_avg.coords['lat'], pr_mean_avg, label='BCC')
axes[0].set_xlabel('', fontsize=12)
axes[0].set_ylabel('precipitation average/(mm/d)', fontsize=12)
axes[0].set_title('平均降水量', fontsize=12)
axes[0].xaxis.set_major_formatter(FormatStrFormatter('%d°N'))
axes[0].set_ylim([0, 50])  # 设置y轴刻度范围

# 绘制pr_frequency变量的纬度变化曲线
axes[1].plot(pr_frequency_avg.coords['lat'], pr_frequency_avg, label='BCC')
axes[1].set_xlabel('', fontsize=12)
axes[1].set_ylabel('precipitation frequency/%', fontsize=12)
axes[1].set_title('降水频率', fontsize=12)
axes[1].xaxis.set_major_formatter(FormatStrFormatter('%d°N'))
axes[1].set_ylim([0, 80])  # 设置y轴刻度范围

# 绘制pr_intensity变量的纬度变化曲线
axes[2].plot(pr_intensity_avg.coords['lat'], pr_intensity_avg, label='BCC')
axes[2].set_xlabel('', fontsize=12)
axes[2].set_ylabel('precipitation intensity/(mm/h)', fontsize=12)
axes[2].set_title('降水强度', fontsize=12)
axes[2].xaxis.set_major_formatter(FormatStrFormatter('%d°N'))
axes[2].set_ylim([0, 4.0])  # 设置y轴刻度范围

# 绘制ERA5数据
axes[0].plot(pr_mean_avg.coords['lat'], tp_average.mean(dim='lon'), label='ERA5')
axes[1].plot(pr_frequency_avg.coords['lat'], tp_frequency.mean(dim='lon'), label='ERA5')
axes[2].plot(pr_intensity_avg.coords['lat'], tp_intensity.mean(dim='lon'), label='ERA5')

# 添加图例
for ax in axes:
    ax.legend()

plt.tight_layout()
plt.show()
Python空间数据分析:使用xarray和geopandas绘制降水量纬度变化曲线

原文地址: http://www.cveoy.top/t/topic/bGnQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录