I am trying to implement a simple mouse click event in matplotlib. I wish to plot a figure then use the mouse to select the lower and upper limits for integration. So far I am able to print the coordinates to screen but not store them for later use in the program. I would also like to exit the connection to the figure after the second mouse click.
Below is the code which currently plots and then prints the coordinates.
My Question(s):
How can I store coordinates from the figure to list? i.e. click = [xpos, ypos]
Is it possible to get two sets of x coordinates in order to do a simple integration over that section of line?
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)
def onclick(event):
global ix, iy
ix, iy = event.xdata, event.ydata
print 'x = %d, y = %d'%(
ix, iy)
global coords
coords = [ix, iy]
return coords
for i in xrange(0,1):
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
question from:https://stackoverflow.com/questions/25521120/store-mouse-click-event-coordinates-with-matplotlib