我有一个循环脚本,它绘制( matplotlib
),然后在绘制后等待 raw_input
在继续之前。这将允许我在循环继续之前删除我不想要/不需要的任何数字。
我尝试了几种不同的方法,但在继续之前,它们似乎从未正确显示过数字。
# test1
x = [1, 2, 3]
plt.ion() # turn on interactive mode
for loop in range(0,3):
y = np.dot(x, loop)
plt.figure()
plt.plot(x,y)
plt.show()
_ = raw_input("Press [enter] to continue.")
^ 这个绘制了图形窗口,但在循环结束之前什么都不显示
#test2
x = [1, 2, 3]
plt.ion() # turn on interactive mode
for loop in range(0,3):
y = np.dot(x, loop)
plt.figure()
plt.plot(x,y)
plt.pause(0.02)
plt.show()
_ = raw_input("Press [enter] to continue.")
^ 这个图形很好地绘制了图形,但是当 raw_input 打开时它会卡住并且图形窗口崩溃。完整循环完成后,窗口将再次恢复正常(可用和可缩放)
我查看了
plt.show(block=False)
命令但没有任何进展有任何想法吗?
备注这个问题与问题here非常相似。但是,正如你从上面的例子中看到的,我没有设法使用这些答案让它工作(建议
plt.ion()
和 plt.pause(0.2)
最佳答案
正如您几乎发现的那样, plt.pause(interval) 确实有效,因为根据 documentation ,
If there is an active figure, it will be updated and displayed before the pause, and the GUI event loop (if any) will run during the pause.
因此,如果您已将暂停间隔增加到以下值:
interval = 10 # units of seconds
plt.pause(interval)
plt.show()
input("Press Enter to continue...")
与情节互动。
程序等待 raw_input() 时的暂停,由
方式已更改为 Python 3.x 中的 input() 并且也可能有所帮助
防止图形窗口崩溃。
关于python - 绘制并等待 raw_input 的 Matplotlib 图形循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37929622/