【问题标题】:United color bar for all subplots in matplotlibmatplotlib 中所有子图的统一颜色条
【发布时间】:2022-01-06 12:45:59
【问题描述】:

我正在尝试在我的情节中添加一个颜色条,其中包含 4 个子情节。 我已经使用此代码手动添加了子图:

extent = [-8, 37, 28, 46]
fig = plt.figure(figsize=(5.5, 3.5), constrained_layout= False)
spec = fig.add_gridspec(ncols=2, nrows=2)
ax0 = fig.add_subplot(spec[0, 0], projection=ccrs.PlateCarree(), frameon= False)
ax0.set_extent(extent)

ax1 = fig.add_subplot(spec[0, 0], projection=ccrs.PlateCarree(), frameon= False)
ax1.set_extent(extent)

我将这些行写到 ax3。 现在我尝试制作一个彩条,使用plt.color bar()

它只在最后一个子图中添加了一个颜色条...我尝试了其他在线解决方案,但它们都使用了我不使用的fig, axes= plt.subplots(...)..

编辑-这个问题How to have one colorbar for all subplots 并没有真正帮助我...... 当我尝试运行此代码时

fig.subplots_adjust(right=0.8)

cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])

fig.colorbar(ax3, cax=cbar_ax)

我得到这个错误- AttributeError:“GeoAxesSubplot”对象没有属性“get_array” 我正在使用 Cartopy 制作全局事件的 hist2d。

有人知道如何克服这个问题吗? 提前致谢, 卡琳。

【问题讨论】:

标签: python matplotlib colorbar


【解决方案1】:

我设法解决了问题...刚刚使用fig, axes =plt.subplots(...) 并将我的代码转换为使用此代码:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多