Basemap改成cartopy。

因为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()
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值