将颜色条添加到 GRIB 图

Adding Color Bar to GRIB Plot

我不知道如何使用以下代码将颜色条添加到我的 GRIB 图中:

grib='/home/rik/hrrr/afr/vis.t20z.f00.grib2'
grbs=pygrib.open(grib)

lats, lons = grb.latlons()

map_crs = ccrs.LambertConformal(central_longitude=-100,
                            central_latitude=35,
                            standard_parallels=(30, 60))
data_crs = ccrs.PlateCarree()

fig = plt.figure(1, figsize=(14,12))
ax = plt.subplot(1, 1, 1, projection=map_crs)
ax.set_extent([-120, -75, 25, 50], data_crs)
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))
ax.add_feature(cfeature.STATES.with_scale('50m'))

ax.contourf(lons, lats, grb.values, transform=data_crs)

plt.title('Surface Visibility')

plt.colorbar(grb.values, orientation='vertical')

执行上面的代码后,我看到绘图中有一个空的垂直颜色条。我错过了什么?

当调用 plt.colorbar() 时,第一个参数是颜色条应该对应的 matplotlib 艺术家——在这种情况下你正在颜色映射 contourf,所以你应该采用 return 值并传递它,如下所示:

cntr = ax.contourf(lons, lats, grb.values, transform=data_crs)
plt.colorbar(cntr, orientation='vertical')