【问题标题】:How to change the height of each image grid with mpl_toolkits.axes_grid1.Imagegrid如何使用 mpl_toolkits.axes_grid1.Imagegrid 更改每个图像网格的高度
【发布时间】:2021-05-23 07:33:14
【问题描述】:

我正在使用 mpl_toolkits.axes_grid1.Imagegrid 绘制具有 4*6 网格的图像:

target = np.reshape(target, (12, 41, 81))
output = np.reshape(output, (12, 41, 81))
pred_error = target - output
# target: C x D x H x W
sfmt = ticker.ScalarFormatter(useMathText=True)
sfmt.set_powerlimits((-2, 2))
cmap = 'jet'
fig = plt.figure(1, (11, 5.5))
axes_pad = 0.1
cbar_pad = 0.1
label_size = 6
plt.rcParams["mpl_toolkits.legacy_colorbar"] = False
subplots_position = [(4,6,i) for i in range(1, 25)]
for i, subplot_i in enumerate(subplots_position):
    if i in [i for i in range(6)]+[i for i in range(12, 18)]:
        # share one colorbar
        grid = ImageGrid(fig, subplot_i,          # as in plt.subplot(111)
                          nrows_ncols=(2, 1),
                          axes_pad=axes_pad,
                          share_all=False,
                          cbar_location="right",
                          cbar_mode="single",
                          cbar_size="3%",
                          cbar_pad=cbar_pad,
                          )
        if i <6:
            data = (target[i], output[i])
        else:
            data = (target[i-6], output[i-6])
        channel = np.concatenate(data)
        vmin, vmax = np.amin(channel), np.amax(channel)
        # Add data to image grid
        for j, ax in enumerate(grid):
            im = ax.imshow(data[j], vmin=vmin, vmax=vmax, cmap=cmap)
            ax.set_axis_off()
        # ticks=np.linspace(vmin, vmax, 10)
        #set_ticks, set_ticklabels
        cbar = grid.cbar_axes[0].colorbar(im, format=sfmt)
        # cbar.ax.set_yticks((vmin, vmax))
        cbar.ax.yaxis.set_offset_position('left')
        cbar.ax.tick_params(labelsize=label_size)
        cbar.ax.toggle_label(True)

    else:
        grid = ImageGrid(fig, subplot_i,  # as in plt.subplot(111)
                          nrows_ncols=(1, 1),
                          axes_pad=axes_pad,
                        #  share_all=True,
                        #  aspect=True,
                          cbar_location="right",
                          cbar_mode="single",
                          cbar_size="6%",
                          cbar_pad=cbar_pad,
                          )
        data = [pred_error[i%12]]
        for j, ax in enumerate(grid):
            im = ax.imshow(data[j], cmap=cmap)
            ax.set_axis_off()
            ax.set_axes_locator
            cbar = grid.cbar_axes[j].colorbar(im, format=sfmt)
            grid.cbar_axes[j].tick_params(labelsize=label_size)
            grid.cbar_axes[j].toggle_label(True)

plt.tight_layout()##pad=0.25, w_pad=0.25, h_pad=0.25)
# fig.subplots_adjust(wspace=0.075, hspace=0.075)
plt.show()
plt.savefig('test.pdf', 
            dpi=300, bbox_inches='tight')

plt.show()
plt.close(fig)

target 为 (2,6,41,81) 形状数组,绘图时调整大小为 (12,41,81),输出 维度相同, pred_error 是它们之间的区别。我想在第 1 行和第 4 行显示 target,在第 2 行和第 5 行显示 output,使用相同的颜色条,在第 3 和第 6 行显示 pred_error .

我现在得到的图像:我注释了网格框,

我想要的图像是没有巨大差距的:

我知道问题在于我图像中的网格大小都相同,但我没有找到编辑这些绿色图像网格高度的方法。感谢您的帮助!!!

【问题讨论】:

  • Imagegrid 的设计使得网格默认大小相同,更改它不会太容易,我没有找到方便的方法。

标签: python matplotlib subplot colorbar


【解决方案1】:

我最终只用子图制作了我想要的数字(仍然保留颜色条共享属性):

target = np.random.randn(12, 41, 81)
target[6:,:,:] *= 2
output = np.random.randn(12, 41, 81)
output[6:,:,:] *= 2

target = np.reshape(target, (12, 41, 81))
output = np.reshape(output, (12, 41, 81))
pred_error = target - output

fig, axes = plt.subplots(nrows=6, ncols=6, figsize=(11, 5))
axes = axes.flat
index = [[i, i+6] for i in range(6)] + [[i, i+6] for i in range(18, 18+6)]
error_index = list(np.arange(12, 18)) + list(np.arange(30, 36))
y_ind = 0
axes_pad = 0.1
cbar_pad = 0.1
label_size = 6
for ind_pair in index:
    axy = axes[ind_pair[0]]
    data = (target[y_ind], output[y_ind])
    vmin = np.min(data)
    vmax = np.max(data)
    imy = axy.imshow(data[0], vmin=vmin, vmax=vmax, cmap = 'jet')
    axout = axes[ind_pair[1]]
    imout = axout.imshow(data[1], vmin=vmin, vmax=vmax, cmap = 'jet')
    axy.set_axis_off()
    axout.set_axis_off()
    v1 = np.linspace(vmin, vmax, 7, endpoint=True)
    cbar = fig.colorbar(imout, ax=[axes[ind_pair[0]], axes[ind_pair[1]]], 
                        format='%.2f', aspect=20, shrink = 0.95)
    cbar.set_ticks(v1)
    cbar.ax.tick_params(labelsize=label_size)
    y_ind += 1

e_ind = 0
for ind_error in error_index:
    axe = axes[ind_error]
    ime = axe.imshow(pred_error[e_ind], cmap = 'jet')
    axe.set_axis_off()
    v1 = np.linspace(np.min(pred_error[e_ind]),np.max(pred_error[e_ind]), 4, endpoint=True)
    cbar = fig.colorbar(ime, ax=axes[ind_error], format='%.2f', aspect=8, shrink = 0.85)
    cbar.set_ticks(v1)
    cbar.ax.tick_params(labelsize=label_size)
            
# plt.tight_layout()
plt.savefig('a.pdf', 
            dpi=300, bbox_inches='tight')
plt.show()
plt.close(fig)

这个图是这样的:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2015-01-05
    相关资源
    最近更新 更多