3

I have two sets of 8 images each given as numpy arrays.

Because I want to use a for loop at some point in my program, I want to display each set as matplotlib subplots in the same cell, unfortunately it does not work (I tried to use the display function from IPython.display, with no success at all).

Here is the code I used :

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

for i, img in enumerate(set1):
    plt.subplot(2, 4, i+1)
    plt.axis('off')
    plt.imshow(img)

for i, img in enumerate(set2):
    plt.subplot(2, 4, i+1)
    plt.axis('off')
    plt.imshow(img)

It only displays the second set.

1 Answer 1

3

What is happening is that you are plotting everything on the same figure. Therefore, when plotting images from set2 you are overwriting the images from set1. You can create a new figure by calling plt.figure() before your for loops:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

plt.figure()
for i, img in enumerate(set1):
    plt.subplot(2, 4, i+1)
    plt.axis('off')
    plt.imshow(img)

plt.figure()
for i, img in enumerate(set2):
    plt.subplot(2, 4, i+1)
    plt.axis('off')
    plt.imshow(img)
Sign up to request clarification or add additional context in comments.

1 Comment

I see ! Thanks a lot for your answer.

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.