Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib show multiple images with for loop [duplicate]

I want to display multiple figure in Matplotlib. Here's 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) to figure(7) will show during the process but only figure(8) stay in the end. How can I see all of the figures at the same time? It confused me that my environment is Ipython notebook when I change the environment to spyder the result will be correct.

like image 887
user2775128 Avatar asked Apr 14 '15 06:04

user2775128


1 Answers

If you want 8 different figures 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 plot 8 different windows with x vs y and all the windows will stay "alive" until you close them.

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

like image 157
Srivatsan Avatar answered Nov 18 '22 04:11

Srivatsan