I am working on a Jupyter notebook with user interactivity. use after pressing a key will be allowed to use the mouse to click on the figure and get (in this example) (r,z) coordinates of the point in the canvas they clicked and then after pushing another click the canvas stops giving those coordinates back if the mouse buttons are clicked again.
At the moment I can start such events but then I cannot stop it..
I created this simplified working examples to
%matplotlib qt5
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-10,10)
y = x**2
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
coords = []
kid = fig.canvas.mpl_connect('key_press_event', on_press)
def on_press(event):
if event.key =='c':
cid = fig.canvas.mpl_connect('button_press_event', on_click)
if event.key =='x':
fig.canvas.mpl_disconnect(cid)
def on_click(event):
global ir, iz
if event.button != 3:
return
if event.inaxes is not None:
ir, iz = event.xdata, event.ydata
print ('r = %f, z = %f'%(
ir, iz))
global coords
coords.append((ir, iz))
in this example after clicking 'c' I can click with the right button of the mouse and get coordinates. If I then click the 'x' key and then right click again I get back the coordinates.
what is wrong?