0

How to get many figures without closing first one in matplotlib one by one. My code is as follows:

import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,4,9,16,25,36,49,64,81,100]
z=[1,3,4,5,19,13,17,12,15,10]
v=[1,2,4,5,7,8,90,3,2,2]
def func(x,y):
    plt.plot(x, y)# I want the first graph to stay and get second one without closing first
    plt.show()
    return
func(x,y)
myinput=int(input())
if myinput==1:
   func(x,z)
else:
    func(x,v)
#something like this i want
mcadams
  • 11
  • 2
  • See https://stackoverflow.com/a/33050617. Try `plt.ion()` – Bonlenfum Mar 02 '18 at 12:32
  • can you plz help me here , i want seperate figures each time without closing the first one – mcadams Mar 02 '18 at 16:27
  • If you don't use MPL in interactive mode, they block until closed. plt.ion is a global setting but block=False is a per-figure choice that seems to work for you - well done for solving it – Bonlenfum Mar 06 '18 at 13:08

2 Answers2

1
#answering myself after spending lot of time
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,4,9,16,25,36,49,64,81,100]
z=[1,3,4,5,19,13,17,12,15,10]
v=[1,2,4,5,7,8,90,3,2,2]
def func(x,y,index):
    fig=plt.figure(index)
    fig1=fig.add_subplot(1,1,1)
    fig1.plot(x, y)
    plt.show(block=False)
    return
my_input=1
while(my_input!=-1):
    my_input=int(input())
    func(x,y,my_input)
mcadams
  • 11
  • 2
1

As a shortened version of @noman's answer:

plt.show(block=False)

For example, I used it like this to generate two plots from one script, without needing to close the first plot:

# first plot
fig = plt.figure()
plt.scatter(.....whatever....)
plt.show(block=False)

# second plot
fig = plt.figure()
plt.line(.....whatever....)
plt.show()
tamtam
  • 641
  • 9
  • 24