Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to show multiple plots in separate windows using matplotlib?

Hello I am plotting a function 10 times and also printing the separate values. I also wanted to plot all the 10 cases separately in separate windows.

So I created a new for loop for the plotting which is still plotting just the first instance and once I close the first one, only then I can see the second one.

I also tried to use plt.hold(true).

Here is what I tried to do-

def signal():
    t1 = np.random.choice(candidates)
    t2 = np.random.choice(candidates)
    t3 = np.random.choice(candidates)
    t4 = np.random.choice(candidates)
    t5 = np.random.choice(candidates)
    y = a * np.exp(-t /t1) + a * np.exp(-t /t2) + a * np.exp(-t /t3) + a * np.exp(-t /t4) + a * np.exp(-t /t5)
return y

for i in range(nsets):
    signalset = []
    signalset.append(signal())
    print(signal())

for i in range (nsets):
    plt.plot(t, signal())
    plt.show()
    plt.hold(True)

Is there any way I could generate 10 plots simultaneously in 10 different windows?

like image 684
zerogravty Avatar asked Feb 24 '17 03:02

zerogravty


2 Answers

Figures have an index plt.figure(n) where n is a number starting at 1.
This allows to later activate an already created figure to plot new stuff to it, but it also allows create several figures in a loop.

In order to show all images simultaneously, use plt.show() at the very end.
In this case you'd do

for i in range(10):
    plt.figure(i+1) #to let the index start at 1
    plt.plot(t, signal())
plt.show()

This create all 10 windows at the end of the script.

like image 71
ImportanceOfBeingErnest Avatar answered Oct 22 '22 15:10

ImportanceOfBeingErnest


You can create new figure windows by specifying a new figure index, for example plt.figure(10). In your case you can use:

for i in range (nsets):
    plt.figure(i) # choose figure i to be the current figure (create it if not already existing)
    plt.plot(t, signal())
    plt.show()
    plt.hold(True)
like image 1
Julien Avatar answered Oct 22 '22 16:10

Julien