全局变量未更新

时间:2020-06-12 15:37:05

标签: python global-variables

我在这里是新来的,也许这很明显,但是我不明白: 我想保存单击的坐标(坐标)以便与它们一起使用,以某种方式不起作用。我仍然在Python 2.7 btw上。

提前谢谢!

import matplotlib.pyplot as plt

plt.close('all')
coords=[]


# Simple mouse click function to store coordinates
def onclick(event):
    global coords, xi,yi
    xi,yi =event.xdata, event.ydata
    coords.append((xi,yi))
    if len(coords) == 2:
        fig.canvas.mpl_disconnect(cid)
        plt.close(1)



    return coords, xi , yi 




x = np.arange(-10,10)
y = x**2

fig = plt.figure(1)
ax = fig.add_subplot(111)
ax.plot(x,y)
plt.show(1)


# Call click func
cid = fig.canvas.mpl_connect('button_press_event', onclick)
print coords


0 个答案:

没有答案