I'm writing a script to crop a part of an image in python with matplotlib. The matplotlib documentation about event handling and picking and this question helped me a lot. But they don't use the coordinates afterwards like I would like to do.
The problem is, that my program doesn't wait for clicks in the plot. The program opens the plot, calls mpl_connect and goes on.
How can I fix this problem, so that the program waits for my input.
My code:
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)
global coords
coords = []
def onclick(event):
ix, iy = event.xdata, event.ydata
print('x = %d, y = %d'%(ix, iy))
coords.append((ix, iy))
if len(coords) == 2:
fig.canvas.mpl_disconnect(cid)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
I appreciate any help. Thank you very much!