【发布时间】: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。
有人知道如何克服这个问题吗? 提前致谢, 卡琳。
【问题讨论】:
-
这能回答你的问题吗? 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' object has no attribute 'get_array' 我想说我正在使用 cartopy 很重要?
-
对不起!我是新来的...在我的问题中添加了它
-
Colorbar 不将坐标区作为参数。如果没有完整的示例,我们将无法帮助您,但 colorbar 的参数通常是 ScalarMappable,无论您进行颜色映射,它都会返回给您。请阅读matplotlib.org/stable/gallery/subplots_axes_and_figures/…
标签: python matplotlib colorbar