Python 如何暂停pylab图形直到按下键或单击鼠标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15582956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:28:12  来源:igfitidea点击:

How to pause a pylab figure until a key is pressed or mouse is clicked?

pythonmatplotlib

提问by user2201369

I am trying to program an animated simulation using pylab and networkx. The simulation is not interesting all the time so most of the time I want it to go fast, however, I want to be able to pause it and look at it when it looks interesting. Pausing the screen until keypress will solve my problem, because I can press the key as fast/slow as I want.

我正在尝试使用 pylab 和 networkx 编写动画模拟。模拟并不是一直都很有趣,所以大多数时候我希望它快速运行,但是,我希望能够暂停它并在它看起来有趣时查看它。暂停屏幕直到按键将解决我的问题,因为我可以按我想要的快/慢按键。

Here's an example situation:

这是一个示例情况:

import numpy as np
import networkx as nx
import pylab as plt
import sys

def drawGraph(matrix):
    plt.clf()
    G = nx.DiGraph(np.array(matrix))
    nx.draw_networkx(G)
    plt.draw()

    plt.pause(1) #I want this pause to be replaced by a keypress
    #so that it pauses as long as I want

A=[[0,1],[1,0]]
B=[[0,1],[0,0]]
x=1
while True:
    if x==1:
        drawGraph(A)
        x=0
    else:
        drawGraph(B)
        x=1

How should I rewrite the plt.pause(1) line, so that the program pauses until keypress?

我应该如何重写 plt.pause(1) 行,以便程序暂停直到按键?

Some approaches suggested in other threads pauses the program, but the picture disappears or doesn't update.

其他线程中建议的一些方法会暂停程序,但图片消失或不更新。

采纳答案by HYRY

The following code stops and restarts with a mouse click. It uses the "TKAgg" backend:

以下代码通过单击鼠标停止并重新启动。它使用“TKAgg”后端:

import numpy as np
import networkx as nx
import matplotlib
matplotlib.use("TkAgg")

import pylab as plt
plt.ion()

fig = plt.figure()

pause = False

def onclick(event):
    global pause
    pause = not pause

fig.canvas.mpl_connect('button_press_event', onclick)

def drawGraph(matrix):
    fig.clear()
    G = nx.DiGraph(np.array(matrix))
    nx.draw_networkx(G)
    plt.draw()

A=[[0,1],[1,0]]
B=[[0,1],[0,0]]
x=1
while True:
    if not pause:
        if x==1:
            drawGraph(A)
            x=0
        else:
            drawGraph(B)
            x=1
    fig.canvas.get_tk_widget().update() # process events

回答by Eric O Lebigot

You can wait until the user presses enter with raw_input().

您可以等到用户按 Enter 键raw_input()

In order for graphs to be displayed, plt.ion()should be added after the imports.

为了显示图形,plt.ion()应在导入后添加。

I don't think that there is any simple platform-independent way of waiting until a key is pressed in Python, but you might want to check this, if Enter is not enough.

我不认为有任何简单的平台无关的方式来等待,直到在 Python 中按下某个键,但是如果 Enter 不够用,您可能需要检查一下。

回答by Marcus Collins

Is there some reason not to use waitforbuttonpress()?

有什么理由不使用waitforbuttonpress()吗?

import matplotlib.pyplot as plt

A=[[0,1],[1,0]]
B=[[0,1],[0,0]]
x=1
while True:
    if x==1:
        drawGraph(A)
        x=0
    else:
        drawGraph(B)
        x=1
    plt.waitforbuttonpress()

This will, as it says, wait for a key or button press for something to happen. It returns values if you want to know more about the event. Very easy.

正如它所说,这将等待按键或按钮按下以发生某些事情。如果您想了解有关事件的更多信息,它会返回值。好简单。

回答by Vinod Krishna

plt.waitforbuttonpress() exits the inactive state as soon as either a key is pressed or the Mouse is clicked. However, the function returns True if a keyboard key was pressed and False if a Mouse was clicked.

plt.waitforbuttonpress() 只要按下某个键或单击鼠标就退出非活动状态。但是,如果按下键盘键,该函数返回 True,如果单击鼠标,则返回 False。

To hold plot until a keyboard key is pressed:

要保持绘图直到按下键盘键:

keyboardClick=False
while keyboardClick != True:
    keyboardClick=plt.waitforbuttonpress()