在python 2.7中等待matplotlib事件
我在StackOverflow和slighlty上找到了这段代码在python 2.7中等待matplotlib事件,python,matplotlib,plot,mouse-picking,Python,Matplotlib,Plot,Mouse Picking,我在StackOverflow和slighlty上找到了这段代码 import numpy as np import matplotlib.pyplot as plt import time x = np.arange(-10,10) y = x**2 x1 = 0 fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x,y) plt.show() def onclick(event): global x1, go
import numpy as np
import matplotlib.pyplot as plt
import time
x = np.arange(-10,10)
y = x**2
x1 = 0
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
plt.show()
def onclick(event):
global x1, go
x1 = event.xdata
print x1
fig.canvas.mpl_disconnect(cid)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
print x1
我想知道如何停止/等待程序,直到我点击该图
因为正如调用mpl_connect时所写的那样,我可以单击该图,但在单击步骤后,我很快获得了输出x1=0,而不是正确的值
如何求解它以获得正确的值
非常感谢你
卢卡删除
fig.canvas.mpl\u断开连接(cid)
来自onclick
Cid = fig.canvas.mpl_connect('button_press_event', onclick)
执行事件和函数之间的连接。也应该在plt之前。show() 问题中提供的示例几乎没有问题。show语句应该在设置图形和连接回调函数的所有调用之后出现。此外,断开连接可能不是您想要的 这是您的代码,经过编辑以生成图形并重复运行连接的函数
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
import time
x = np.arange(-10,10)
y = x**2
x1 = 0
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
def onclick(event):
global x1, go
x1 = event.xdata
print x1
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()