1

I have an array of shape (7,4,100,100) which means 7 images of size 100x100 with depth 4. I want to display all of them on a single plot. I tried following using 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()

But it squeezes the images in the plot as shown in the figure below where image at left is a single image and other one is the plot of multiple images. any solution or any other approach? enter image description here

5
  • 3
    Please fix the indentation issues in your code. Commented May 9, 2017 at 11:11
  • 1
    Please also provide short example of input_arr Commented May 9, 2017 at 11:13
  • 3
    Question seeking debugging help ("why isn't the code working?") need to provide a minimal reproducible example. In this case is really easy, just use some random data. Commented May 9, 2017 at 11:14
  • What do you mean by squeeze? If it means what I think it means, you could try something like plt.subplot(3, 3, i+1) instead. Commented May 9, 2017 at 12:04
  • @Jaime Please check the updated question. Commented May 9, 2017 at 12:16

1 Answer 1

1

When you moved the axis with np.moveaxis, you already indexed the input array to get only the i-th component of the array. So when you then use imshow, you don't need to plot the i-th index of array2, but the whole of 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()

You can confirm this by printing the shape of array2 and array2[i]

print array2.shape
# (100, 100, 4) 
print array2[i].shape
# (100, 4)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.