简体   繁体   English

循环暂停python代码并检查图形窗口

[英]Pause python code in a loop and inspect figure window

I am trying to plot some figures (matplotlib) in a for loop and plotting the result in a figure window. 我试图在for循环中绘制一些图形(matplotlib),然后在图形窗口中绘制结果。 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. 我正在使用Spyder和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)

Using plt.draw() should do it. 使用plt.draw()应该可以做到。 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.")

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM