如何在不关闭窗口的情况下释放matplolib执行

时间:2016-08-15 19:29:04

标签: python user-interface matplotlib

我想使用imshow()for循环显示一系列图像,但我想在继续之前等待图像上的用户输入(不在终端上)。以下确实有效但它需要关闭窗口,这不是最佳的,因为我的for循环有超过一千次迭代。如何将plt.clf()暂时解除阻塞直到下一个循环。

x, y, z = sp.mgrid[0:10, 0:100, 0:100]

for img in x:
    f = plt.figure()
    def onkey(event):
        print("pressed {}".format(event))
        plt.close(f)
    f.canvas.mpl_connect("key_press_event", onkey)
    plt.imshow(img)
    plt.show()
    print("continuing")

谢谢!

1 个答案:

答案 0 :(得分:0)

使用:

plt.ion()
plt.show()
循环前

,然后删除plt.close()&循环中plt.show()

要阻止循环直到发生按下事件,请添加:

f.canvas.start_event_loop(timeout=-1)
循环内的