python - 绘制并等待 raw_input 的 Matplotlib 图形循环

标签 python python-2.7 matplotlib

我有一个循环脚本,它绘制( 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/

    相关文章:

    python - 让一个类表现得像 Python 中的一个列表

    将值循环到变量中的 Pythonic 方式

    python - 使用 matplotlib 在箱线图中显示传单颜色

    python - 在每一步的 for 循环中绘制 matplotlib 图形

    python - 正则表达式用于匹配不具有相同单词出现在另一个关键字之前的关键字

    python - 从 Python 2.7 读取 .mpp 文件

    python - 如何将新的 "Conn Types"添加到 Airflow 2.0?

    python - 如何在一定数量的请求后停止 scrapy 蜘蛛?

    python - py2exe 与 scipy.sparse.sparsetools.csr

    python - 使用 Matplotlib 将分类变量转换为颜色