0

I am trying to plot some figures (matplotlib) in a for loop and plotting the result in a figure window. The figure window updates after every loop. However, the figure window is unresponsive or blank and does not show any plot until the loop is terminated. I am using a Spyder and Python 3.7.

Any suggestions?

Edit: Below is some sample code

import numpy as np
import matplotlib.pyplot as plt
import time

x = np.linspace(0,2*np.pi,100)
for amp in range(10):
    print(amp)
    y = amp*np.cos(x)
    plt.figure(1)    
    plt.plot(x,y)
    time.sleep(1)
SEU
  • 1,304
  • 4
  • 15
  • 36

2 Answers2

2

Using plt.draw() should do it. Try something like this

import numpy as np
import matplotlib.pyplot as plt
import time

plt.show()

x = np.linspace(0,2*np.pi,100)
for amp in range(10):
    print(amp)
    y = amp*np.cos(x)
    plt.plot(x,y)
    plt.draw()
    plt.pause(0.001)
    input("Press [enter] to continue.")
Arsal
  • 447
  • 2
  • 6
0

Second answer from this question migh help you:

Update Figure

import matplotlib.pyplot as plt
import numpy as np

plt.ion()
for i in range(50):
    y = np.random.random([10,1])
    plt.plot(y)
    plt.draw()
    plt.pause(0.0001)
    plt.clf()