【问题标题】:Can AxesGrid be used to plot two imshows (overlay) with two separate colorbars?可以使用 AxesGrid 绘制两个带有两个单独颜色条的 imshow(叠加)吗?
【发布时间】:2023-04-09 04:17:01
【问题描述】:

我在 matplotlib 中使用 AxesGrid 创建一个覆盖两个 imshow 图的图,每个图像颜色图都有一个并排的单独颜色条。虽然我可以在 this question/answer 中看到使用 pyplot.colorbar() 会自动在第一个颜色条旁边绘制第二个颜色条,但这似乎不适用于 AxesGrid。

    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1 import AxesGrid

    fig = plt.figure(1, facecolor='white',figsize=(10,7.5))

    grid = AxesGrid(fig, 111, 
                    nrows_ncols=(1, 1),
                    axes_pad=(0.45, 0.15),
                    label_mode="1",
                    share_all=True,
                    cbar_location="right",
                    cbar_mode="each",
                    cbar_size="7%",
                    cbar_pad="2%",
                    )

    im = grid[0].imshow(my_image, my_cmap)
    cbar = grid.cbar_axes[0].colorbar(im)

    im2 = grid[0].imshow(my_image_overlay, my_cmap2, alpha=0.5)
    cbar2 = grid.cbar_axes[0].colorbar(im2)

    plt.show()

但是,这仅显示第二个颜色条。 (大概覆盖了第一个)。我尝试使用以下内容覆盖 cbar2 中的填充:

    cbar2 = grid.cbar_axes[0].colorbar(im2, pad=0.5)

但这会导致“得到一个意外的关键字参数'pad'”的错误

有没有办法抵消第二个颜色条?

【问题讨论】:

    标签: python matplotlib colorbar


    【解决方案1】:

    我认为您可能需要为颜色条制作两个轴并使用它们:

    from mpl_toolkits.axes_grid1 import make_axes_locatable
    # create an axes on the right side of ax. The width of cax will be 5%
    # of ax and the padding between cax and ax will be fixed at 0.05 inch.
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    

    因为您当前为两者使用相同的分配空间。

    【讨论】:

      猜你喜欢
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 2021-01-17
      • 2019-09-28
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      相关资源
      最近更新 更多