子图未对齐 [英] Subplots not aligned

查看:43
本文介绍了子图未对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对齐我的子图.我尝试了几种组合,但这些是我得到的最接近的.我只需要数字彼此靠近并在行的左端有标题.这个所谓的简单任务让我遗漏了什么?

I am trying to align my subplots. I have tried several combinations but these are the closest that I get. I simply need the figures to be close to each other and have the title on the left end of the row. What am I missing from this supposedly easy task?

此变体增加了不均匀的间距.

This variant adds uneven spacing.

# Input
fig, axs = plt.subplots(1,10,figsize=(15,3), squeeze=False)
fig.subplots_adjust(hspace=0, wspace=0.1)
fig.suptitle('Input', x=0.1, y=0.6)
for i in range(1):
    for j in range(10):
        im = x_true_np[1,i*20+j]
        axs[i][j].imshow(im, cmap="gray")
        axs[i][j].axis('off')
# Ground Truth
fig, axs = plt.subplots(1,10,figsize=(15,3), squeeze=False)
fig.subplots_adjust(hspace=0, wspace=0.1)
fig.suptitle('Ground Truth', x=0.1, y=0.6)
for i in range(1):
    for j in range(0,10):
        im = x_true_np[1,i*20+j+10]
        axs[i][j].imshow(im, cmap="gray")
        axs[i][j].axis('off')
# Predictions
fig, axs = plt.subplots(1,10,figsize=(15,3), squeeze=False)
fig.subplots_adjust(hspace=0, wspace=0.1)
fig.suptitle('Predictions', x=0.1, y=0.6)
for i in range(1):
    for j in range(0,10):
        im = x_pred_np[1,i*20+j]
        axs[i][j].imshow(im, cmap="gray")
        axs[i][j].axis('off')

此变体忽略hspace参数,甚至不显示标题.

This variant ignores the hspace parameter and doesn't even display the titles.

fig, axs = plt.subplots(3,10,figsize=(15,10), squeeze=False)
fig.subplots_adjust(hspace=0, wspace=0.1)
axs[0][0].set_ylabel("Input", fontsize=20)
for j in range(10):
    im = x_true_np[1,j]
    axs[0][j].imshow(im, cmap="gray")
    axs[0][j].axis('off')
axs[1][0].set_ylabel("Ground Truth", fontsize=20)
for j in range(10):
    im = x_true_np[1,10+j]
    axs[1][j].imshow(im, cmap="gray")
    axs[1][j].axis('off')
axs[2][0].set_ylabel("Ground Truth", fontsize=20)
for j in range(10):
    im = x_pred_np[1,j]
    axs[2][j].imshow(im, cmap="gray")
    axs[2][j].axis('off')    
fig.tight_layout() 

推荐答案

我最终使用了 ImageGrid 工具箱.轻松多了.

I ended up using the ImageGrid toolbox. So much easier.

from mpl_toolkits.axes_grid1 import ImageGrid

idxs = [6,7,8,9]

for idx in idxs:
    fig  = plt.figure(idx, (15, 10))
    grid = ImageGrid(fig, 111, nrows_ncols=(3, 10), axes_pad=0.1)
    for i in range(3):
        grid[0].set_ylabel("Input")
        grid[0].set_ylabel("Ground Truth")
        grid[0].set_ylabel("Prediction")
        for j in range(10):
            grid[j].imshow(x_true_np[idx,j], cmap="gray")
            grid[j+10].imshow(x_true_np[idx,j+10], cmap="gray")
            grid[j+20].imshow(x_pred_np[idx,j], cmap="gray")

这篇关于子图未对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆