Matplotlib shows multiple images with a for loop

I want to display several shapes in Matplotlib. Here is my code:

for i in range(8): a = sitk.ReadImage("000%d.IMA"%(i+1)) ... plt.figure(i+1) plt.imshow(a_np,cmap=plt.cm.gray) 

However, Figure (1) in Figure (7) will be displayed during the process, but only the number (8) will remain at the end. How can I see all the numbers at once? I was confused that my environment is an Ipython laptop, when I change the environment to spyder, the result will be correct.

+6
source share
1 answer

If you need 8 different digits in 8 different windows, here is an example that will work:

 import matplotlib.pyplot as plt import numpy as np x = np.linspace(0,10) y = np.sin(x) for i in range(8): plt.plot(x,y) plt.figure(i+1) plt.show() 

This will display 8 different windows with x vs y and all windows will remain β€œalive” until you close them.

Make sure you call plt.show() outside the for loop

+8
source

Source: https://habr.com/ru/post/985097/


All Articles