因为mac安装不上basemap。画图也是在自己电脑上画图方便些。所以将basemap改成cartopy。
由于你不能使用 Basemap,我建议使用 cartopy 库作为替代。cartopy 是一个用于绘制地图和进行地理空间数据分析的Python库,与Basemap类似,但得到了更好的维护和支持。
修改之前--点击查看代码
# ------------------------------------------------------------------
# Figure 2: spatial distributions for predictions and observations
# ------------------------------------------------------------------
# if plt_f in ['Fig.2']:
# plt.figure
# #global
# plt.subplot(1,2,1)
# lon, lat = np.meshgrid(lon_, lat_)
# m = Basemap(llcrnrlon=np.min(lon),
# llcrnrlat=np.min(lat),
# urcrnrlon=np.max(lon),
# urcrnrlat=np.max(lat))
# m.drawcoastlines()
# m.drawcountries()
# parallels = np.arange(-90.,90,18.)
# meridians = np.arange(-180.,179.,36.)
# m.drawparallels(parallels, labels=[False, True, True, False], dashes=[1, 400])
# m.drawmeridians(meridians, labels=[True, False, False, True], dashes=[1, 400])
# xi, yi = m(lon, lat)
# print('--------------------')
# print(xi)
# y_pred_pltday = y_pred[pltday, :,:]
# y_pred_pltday[mask==0]=-9999
# if cfg['label'] == ["volumetric_soil_water_layer_1"] or cfg['label'] == ["volumetric_soil_water_layer_2"] or cfg['label'] == ["volumetric_soil_water_20cm"]:
# cs = m.contourf(xi,yi, y_pred_pltday, np.arange(0, 0.6, 0.05), cmap='YlGnBu')
# cbar = m.colorbar(cs, location='bottom', pad="10%")
# cbar.set_label('m$^{3}$/m$^{3}$')
# elif cfg['label'] == ["surface_sensible_heat_flux"]:
# cs = m.contourf(xi,yi, y_pred_pltday, np.arange(-140, 141, 20), cmap='jet')
# cbar = m.colorbar(cs, location='bottom', pad="10%")
# cbar.set_label('W/m$^{2}$')
# plt.title(name_pred)
#
# # observations
# plt.subplot(1,2,2)
# m = Basemap(llcrnrlon=np.min(lon),
# llcrnrlat=np.min(lat),
# urcrnrlon=np.max(lon),
# urcrnrlat=np.max(lat))
# m.drawcoastlines()
# m.drawcountries()
# parallels = np.arange(-90.,90.,18.)
# meridians = np.arange(-180.,179.,36.)
# m.drawparallels(parallels, labels=[False, True, True, False], dashes=[1, 400])
# m.drawmeridians(meridians, labels=[True, False, False, True], dashes=[1, 400])
# xi, yi = m(lon, lat)
# print('xi is',xi)
# y_test_pltday = y_test[pltday, :,:]
# y_test_pltday[mask==0]=-9999
# if cfg['label'] == ["volumetric_soil_water_layer_1"] or cfg['label'] == ["volumetric_soil_water_layer_2"] or cfg['label'] == ["volumetric_soil_water_20cm"]:
# cs = m.contourf(xi,yi, y_test_pltday, np.arange(0, 0.6, 0.05), cmap='YlGnBu')
# cbar = m.colorbar(cs, location='bottom', pad="10%")
# cbar.set_label('m$^{3}$/m$^{3}$')
# elif cfg['label'] == ["surface_sensible_heat_flux"]:
# cs = m.contourf(xi,yi, y_test_pltday, np.arange(-140, 141, 20), cmap='jet')
# cbar = m.colorbar(cs, location='bottom', pad="10%")
# cbar.set_label('W/m$^{2}$')
# plt.title(name_test)
# #plt.savefig(out_path + name_test + '_spatial distributions.png')
# print('Figure 2 : spatial distributions for predictions and observations completed!')
# plt.show()
修改之后--点击查看代码
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
# 确保这些变量已经定义
# lon_, lat_, y_pred, pltday, mask, cfg, name_pred, y_test, name_test
if plt_f in ['Fig.2']:
# 设置图形的大小
plt.figure(figsize=(12, 6))
# 第一个子图 - 预测
ax1 = plt.subplot(1, 2, 1, projection=ccrs.PlateCarree())
ax1.coastlines()
ax1.set_global()
xi, yi = np.meshgrid(lon_, lat_)
y_pred_pltday = y_pred[pltday, :, :]
y_pred_pltday[mask == 0] = np.nan # 使用NaN来处理无数据的区域
# 根据不同的配置绘制不同的数据
if cfg['label'] in [["volumetric_soil_water_layer_1"], ["volumetric_soil_water_layer_2"], ["volumetric_soil_water_20cm"]]:
cs = ax1.contourf(xi, yi, y_pred_pltday, np.arange(0, 0.6, 0.05), cmap='YlGnBu')
cbar = plt.colorbar(cs, orientation='horizontal', pad=0.05, aspect=50)
cbar.set_label('m$^3$/m$^3$')
elif cfg['label'] == ["surface_sensible_heat_flux"]:
cs = ax1.contourf(xi, yi, y_pred_pltday, np.arange(-140, 141, 20), cmap='jet')
cbar = plt.colorbar(cs, orientation='horizontal', pad=0.05, aspect=50)
cbar.set_label('W/m$^2$')
plt.title(name_pred)
# 第二个子图 - 观测
ax2 = plt.subplot(1, 2, 2, projection=ccrs.PlateCarree())
ax2.coastlines()
ax2.set_global()
y_test_pltday = y_test[pltday, :, :]
y_test_pltday[mask == 0] = np.nan # 使用NaN来处理无数据的区域
# 根据不同的配置绘制不同的数据
if cfg['label'] in [["volumetric_soil_water_layer_1"], ["volumetric_soil_water_layer_2"], ["volumetric_soil_water_20cm"]]:
cs = ax2.contourf(xi, yi, y_test_pltday, np.arange(0, 0.6, 0.05), cmap='YlGnBu')
cbar = plt.colorbar(cs, orientation='horizontal', pad=0.05, aspect=50)
cbar.set_label('m$^3$/m$^3$')
elif cfg['label'] == ["surface_sensible_heat_flux"]:
cs = ax2.contourf(xi, yi, y_test_pltday, np.arange(-140, 141, 20), cmap='jet')
cbar = plt.colorbar(cs, orientation='horizontal', pad=0.05, aspect=50)
cbar.set_label('W/m$^2$')
plt.title(name_test)
# 显示图形
plt.show()