在单个图中显示来自numpy数组的多个图像

时间:2017-05-09 11:10:36

标签: python numpy matplotlib

我有一个形状数组(7,4,100,100),这意味着7个尺寸为100x100且深度为4的图像。我想在一个图上显示所有这些图像。我尝试使用matplotlib:

    input_arr=numpy.load(r'C:\Users\x\samples.npy')

    for i, el in enumerate(input_arr):
        #moving axis to use plt: i.e [4,100,100] to [100,100,4]
        array2= numpy.moveaxis(input_arr[i],0,-1)
        plt.subplot(3,3, i + 1), plt.imshow(array2[i])
    plt.show()

但是它会压缩图中的图像,如下图所示,左边的图像是单个图像,另一个是多个图像的图。任何解决方案或任何其他方法? enter image description here

1 个答案:

答案 0 :(得分:1)

当您使用np.moveaxis移动轴时,您已经将输入数组编入索引以仅获取数组的 i-th 组件。因此,当您使用imshow时,您不需要绘制array2 i-th 索引,而是整个array2

for i, el in enumerate(input_arr):
    #moving axis to use plt: i.e [4,100,100] to [100,100,4]
    array2 = numpy.moveaxis(input_arr[i], 0, -1)
    plt.subplot(3, 3, i + 1)
    plt.imshow(array2)      # <- I changed this line
plt.show()

您可以通过打印array2array2[i]

的形状来确认
print array2.shape
# (100, 100, 4) 
print array2[i].shape
# (100, 4)