You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python/Cartopy绘制气溶胶气候图时设置plt.colorbar数值范围的问题

固定colorbar数值范围的实现方法

你可以通过在plt.contourf()方法中添加vmin、vmax两个参数手动指定颜色映射的上下限,即可将colorbar的范围固定为你需要的0到3:

  • vmin:指定颜色映射的最小值,填入0
  • vmax:指定颜色映射的最大值,填入3

修改后的完整代码如下:

import matplotlib.pyplot as plt
from netCDF4 import Dataset as netcdf_dataset
import numpy as np

from cartopy import config
import cartopy.crs as ccrs
import cartopy.feature as cfeature

AODs_Season = netcdf_dataset('/home/vescovint/Documents/AOD_0219_DJF.nc')
lats = AODs_Season.variables['lat'][:]
lons = AODs_Season.variables['lon'][:]
AODS = AODs_Season.variables['AOD_550_Dark_Target_Deep_Blue_Combined_Mean_Mean'][0, :, :]

ax = plt.axes(projection=ccrs.PlateCarree())

# 新增vmin和vmax参数固定数值映射范围
plt.contourf(lons, lats, AODS, 60, transform=ccrs.PlateCarree(), cmap='YlOrRd', vmin=0, vmax=3)
plt.colorbar(shrink=0.55)
plt.gcf().set_size_inches(15, 11)
ax.coastlines()

plt.savefig('/home/vescovint/Documents/AOD_Maps/DJF2002')

如果需要标识出超过最大值3的数值,可以额外在plt.contourf()的参数中添加extend='max',生成的colorbar顶部会出现三角标识,代表对应区域数值超过了设置的最大值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 19:18:01