使用for循环显示多个图像-Matplotlib

时间:2019-11-03 03:30:25

标签: python matplotlib

我想显示89张图像,并且每张图像都存储为numpy数组,形状为[89,14 * 79]

我使用重塑[79,14]重塑了每个数据,并且必须显示整个图像。

由于我想每行显示5张图像,所以我总共要有18行。

我的代码在下面

for i in range(89):
    pixels=cp_cfd[i,:].reshape(79, 14)
    plt.subplot(1,5,i+1)
    plt.imshow(pixels, cmap=plt.cm.viridis)
    plt.title('#'+str(i+1))
plt.show()

使用plt.subplot(1,5,i+1)时出现错误。而且第一行只有五张图片。

ValueError: num must be 1 <= num <= 5, not 6

我知道我应该对整个图像使用plt.subplot(18,5,i+1)而不是plt.subplot(1,5,i+1),但是当我使用plt.subplot(18,5,i+1)时,图像的尺寸会变得太小。 我想显示全部89张图像,但是使用plt.subplot(1,5,i+1)

时要显示它们的大小

请帮助我。

1 个答案:

答案 0 :(得分:0)

哦,我还是解决了我自己的问题。

cfd_last = x_cfd.shape[0]

for i in range(18): # number of rows of plot
    for j in range(5):

        if i*5+j == cfd_last:
            break

        pixels=cp_cfd[i*5+j,:].reshape(79, 14)
        plt.subplot(1,5,j+1)
        plt.imshow(pixels, cmap=plt.cm.jet)
        plt.title('#'+str(i*5+j+1))


    plt.show()