从numpy数组在matplotlib中显示多个图像

2024-06-02 07:51:54 发布

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

我有一个形状为(164224224)的数组。64个224*224大小的单通道图像。当我这样做时:

plt.imshow(output_image[0,1,:,:], interpolation='nearest')

图像显示正确。

但当我这么做的时候:

for i in range(64):
    plt.imshow(output_image[0,i,:,:], interpolation='nearest')

我只看到一个图像作为结果,即使有64个图像。

如何获取64个图像的行?我做错什么了?


Tags: in图像imageforoutputrangeplt数组
2条回答

此函数为每个图像创建子块:
img的形状为(n,height,width,channel)

import numpy as np
def picshow(img):
    num = len(img)
    ax = np.ceil(np.sqrt(num)) 
    ay = np.rint(np.sqrt(num)) 
    fig =plt.figure()
    for i in range(1,num+1):
        sub = fig.add_subplot(ax,ay,i)
        sub.set_title("titre",i)
        sub.axis('off')
        sub.imshow(data[i-1])
    plt.show()

可以为每个图像创建新的子块:

fig = plt.figure(figsize=(50, 50))  # width, height in inches

for i in range(64):
    sub = fig.add_subplot(64, 1, i + 1)
    sub.imshow(output_image[0,i,:,:], interpolation='nearest')

这将把所有64个图像放在一个列中。更改为:

sub = fig.add_subplot(8, 8, i + 1)

八列八行。

相关问题 更多 >