5

The following code

%matplotlib inline

for i in range(0, 5):
    index = np.random.choice(len(dataset))
    print('index:', index)
    image = dataset[index, :, :]
    print('image shape:', np.shape(image))
    plt.imshow(image)

display five printouts and one single image at the end in jupyter notebook.

Is it possible, to display images on each loop iteration?

I was able to do this with image files with

for fullname in fullnames:
  print('fullname:', fullname)
  display(Image(filename=fullname))

Is it possible to do the same with ndarrays?

UPDATE

Writing

for i in range(0, 5):
   ...
   plt.figure()
   plt.imshow(image)

made it better, but not perfect. Images displayed in multiple, but all are AFTER the text.

Should be interleaved.

1 Answer 1

17

Try:

for i in range(0, 5):
   ...
   plt.figure()
   plt.imshow(image)
   plt.show()

Without plt.show() the figures are only rendered and displayed after the cell finishes being executed (i.e., exits the for loop). With plt.show() you force rendering after every iteration.

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.