图像网格,只在某些子图中使用彩色条

2024-04-26 21:16:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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')

这就产生了这个数字:

figure

这显然是不对的,因为每个子批次都有自己的颜色条,即使它不是彩色的。我希望只有cf和{}的色条应该是不同的。有可能吗?在


Tags: fromimport颜色asnppltgridf1
1条回答
网友
1楼 · 发布于 2024-04-26 21:16:18

您必须修改您的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)

enter image description here

为了显示带有所有标签的colorbar,我稍微扩展一下您的图形f1=plt.figure(figsize=(9.5,9))

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

相关问题 更多 >