Matplotlib正确的图形尺寸长宽比

时间:2019-06-04 21:32:18

标签: matplotlib

给出一个图像列表,每个图像的大小为(H,W),给定的图为(n_rows,n_cols)。有计算正确图形尺寸的公式吗?例如,下面的代码如下:

images = [np.random.rand(20, 22) for i in range(32)]

fig = plt.figure(figsize=(6, 14))
axes = [fig.add_subplot(9,3,i+1) for i in range(9)]

for c, ax in enumerate(axes):
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ax.imshow(images[c])

fig.subplots_adjust(wspace=0, hspace=0)

enter image description here

我想消除图像之间的间隙。因此,手动发现figsize =(6,17)产生了我想要的:

enter image description here

1 个答案:

答案 0 :(得分:1)

如此简单:

fig = plt.figure(figsize=(6, 14))
axes = [fig.add_subplot(9,3,i+1) for i in range(9)]

fig.subplots_adjust(wspace=0, hspace=0)
for c, ax in enumerate(axes):
    ax.set_xticklabels([])
    ax.set_yticklabels([])
    ax.imshow(images[c], aspect='auto')
                                  ^^

输出:

enter image description here