Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display ndarray images inline during loop in IPython?

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.

like image 448
Dims Avatar asked Dec 02 '22 14:12

Dims


1 Answers

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.

like image 141
Gustavo Bezerra Avatar answered Dec 28 '22 21:12

Gustavo Bezerra