You need to enable JavaScript to run this app. Geopandas绘制多年月度子图地图时如何统一跨图与全子图色条刻度
优惠活动
大模型
产品
解决方案
定价
更多

Geopandas绘制多年月度子图地图时如何统一跨图与全子图色条刻度

色条不统一的核心根因
  • geopandas的plot()方法默认会对每次单独绘制的图层,自动基于当前列的实际数据极值计算色彩拉伸范围,不会全局复用统一映射规则。你当前所有子图调用时没指定固定范围,旱季、雨季的NDVI取值差异本身就大,自然每个子图的色彩对应关系都不一样。
  • 你虽然在代码里定义了自定义的norm和分档范围,但这套参数从来没有传入任何一次map_df.plot()的调用,等于没有生效。
  • 你通过plt.gca().get_children()[0]获取色彩映射对象的方式,实际拿到的是最后绘制的12月子图的自动拉伸后的图层,哪怕色条刻度写了0~10000,色彩和数值的对应关系完全错配。
可直接套用的修改方案

全局统一定义色彩规则

把需要固定的色卡、归一化范围定义在年份循环外面,只写一次,避免重复定义:

# 全局固定色彩配置,所有图、所有子图统一使用
cmap = matplotlib.cm.viridis
# 保留你原来的分档色条逻辑
bounds = [0, 2500, 5000, 7500, 10000]
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
# 如果需要连续色条,把上面两行替换成下面这行即可:
# norm = matplotlib.colors.Normalize(vmin=0, vmax=10000)

给每个绘图调用强制传入统一映射规则

所有调用map_df.plot()的环节,必须显式传入刚才定义的cmap和norm参数,强制关闭自动缩放,严格按0~10000的固定范围渲染色彩。

修正色条绑定的图层对象

不要从当前轴里随意抓取子对象,直接用最后一次plot()返回的图层对象绑定色条,因为所有图层都用了同一个norm,色彩逻辑完全一致。

修改后的核心代码片段
# 全局定义的固定色彩配置(放在循环前面)
cmap = matplotlib.cm.viridis
bounds = [0, 2500, 5000, 7500, 10000]
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)

years = [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]

for year in years:
    print(year)
    fig, axes = plt.subplots(nrows=6, ncols=2, figsize=(2.5,6.5))

    # 每个plot调用都传入统一的norm,禁止自动缩放
    ax1 = map_df.plot(ax=axes[0, 0], column='modis_jan_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax2 = map_df.plot(ax=axes[1, 0], column='modis_feb_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax3 = map_df.plot(ax=axes[2,0], column='modis_mar_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax4 = map_df.plot(ax=axes[3,0], column='modis_apr_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax5 = map_df.plot(ax=axes[4,0], column='modis_may_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax6 = map_df.plot(ax=axes[5,0], column='modis_jun_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax7 = map_df.plot(ax=axes[0,1], column='modis_jul_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax8 = map_df.plot(ax=axes[1,1], column='modis_aug_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax9 = map_df.plot(ax=axes[2,1], column='modis_sep_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax10 = map_df.plot(ax=axes[3,1], column='modis_oct_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax11 = map_df.plot(ax=axes[4,1], column='modis_nov_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)
    ax12 = map_df.plot(ax=axes[5,1], column='modis_dec_mean_' + str(year), 
                      cmap=cmap, norm=norm, linewidth=0.1)

    # 直接取最后一次plot返回的图层,避免取错对象
    im = ax12.collections[0]
    cax = fig.add_axes([0.1,0.05,0.8,0.03])

    # 色条直接用固定的bounds刻度
    fig.colorbar(im, cax=cax, orientation='horizontal', ticks=bounds, spacing='uniform')

    fig.suptitle(f'{year} MODIS NDVI', y=0.98)
    # 加bbox_inches参数避免裁切手动加的色条,用close代替clf回收内存
    plt.savefig(f'modis_NDVI_{year}.png', format='PNG', dpi=150, bbox_inches='tight')
    plt.close()
调试注意事项
  • 不要再用plt.gca().get_children()[0]这种方式取图层,很容易误把边界线、矢量轮廓当成色彩映射的对象,直接从plot返回的轴对象的collections里取绘图图层是最稳妥的。
  • 如果需要更精细的色条刻度,只需要修改bounds数组的取值即可,所有子图会自动匹配新的固定范围。
  • 循环结束后用plt.close()代替plt.clf(),内存回收更彻底,跑多年份批量绘图时不会出现内存占用越来越高的问题。
  • 保存图时加上bbox_inches='tight'参数,不会把手动放置的色条、标题给自动裁掉。

内容的提问来源于stack exchange,提问作者GeoMonkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.29 06:15:08