I can't figure out how to add a color bar to my GRIB plot using the following code:
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')
After executing the above code I see the plot with an empty vertical color bar. What am I missing?
When calling plt.colorbar()
, the first argument is the matplotlib artist that the colorbar should correspond with--in this case you're colormapping contourf
, so you should take the return value from this method and pass it, like this:
cntr = ax.contourf(lons, lats, grb.values, transform=data_crs)
plt.colorbar(cntr, orientation='vertical')