【问题标题】:ImageGrid with colorbars only on some subplots仅在某些子图上带有颜色条的 ImageGrid
【发布时间】:2017-07-30 03:36:26
【问题描述】:

我正在使用 this answer 设置 3x3 网格来绘制我的数据。我专门选择了这种方法,因为它适用于tight_layout()

但是,我正在阅读 AxesGrid 工具包上的文档,但我不知道如何将颜色条仅放在最右边的图上。

到目前为止,这是我所拥有的:

from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.axes_grid1 import ImageGrid

letters='abcdefghi'
f1=plt.figure(figsize=(9,9))
grid = ImageGrid(f1, 111,
                nrows_ncols=(3,3),
                axes_pad=0.05,
                share_all=True,
                cbar_location="right",
                cbar_mode="each",
                cbar_size="2%",
                cbar_pad=0.15)

A=np.random.rand(10,10)

for i,axis in enumerate(grid):
    im=axis.imshow(A)
    axis.annotate(s=letters[i], xy=(0.1, .85), xycoords='axes fraction', bbox=dict(boxstyle="square", fc="w", alpha=0.9))
    if i in (2,5,8):
        axis.cax.colorbar(im)
f1.tight_layout()
f1.savefig('example.png')

这会产生这个数字:

这显然是不对的,因为每个子图都有自己的颜色条,即使它没有着色。我希望只有 cfi 的颜色条应该不同。这可能吗?

【问题讨论】:

    标签: python matplotlib colorbar


    【解决方案1】:

    您必须修改您的ImageGrid。您将cbar_mode 设置为each,因此所有图像都有自己的颜色条,将其设置为edge,并将方向设置为row(一排图像一个颜色条):

    grid = ImageGrid(f1, 111,
                    nrows_ncols=(3,3),
                    axes_pad=0.05,
                    share_all=True,
                    cbar_location="right",
                    cbar_mode='edge',
                    direction = 'row',
                    cbar_size="2%",
                    cbar_pad=0.15)
    

    为了显示带有所有标签的颜色条,我稍微扩展了你的图f1=plt.figure(figsize=(9.5,9))

    matplotlib 教程中有一个边缘颜色条的例子:https://matplotlib.org/examples/axes_grid/demo_edge_colorbar.html

    【讨论】:

    • 这很简单。我想知道为什么 AxesGrid 文档中没有。该页面中没有“边缘”一词。
    猜你喜欢
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多