【问题标题】:Adding Color Bar to GRIB Plot将颜色条添加到 GRIB 图
【发布时间】:2020-11-13 18:16:29
【问题描述】:

我不知道如何使用以下代码向我的 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')

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

【问题讨论】:

  • 看看专为地理数据可视化而设计的cartopy。您还可以查看与 matplotlib 一起使用的 xarray。

标签: python matplotlib grib


【解决方案1】:

调用plt.colorbar() 时,第一个参数是颜色条应该对应的matplotlib 艺术家——在这种情况下,您正在对contourf 进行颜色映射,因此您应该从该方法获取返回值并传递它,例如这个:

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

【讨论】:

    猜你喜欢
    • 2016-06-22
    • 2015-08-16
    • 2014-01-12
    • 2022-12-12
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多