0

I am using matplotlib and have grabbed much of the code from previous questions within the stack community.

Link here :Store mouse click event coordinates with matplotlib

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import trapz

def find_nearest(array,value):
    idx = (np.abs(array-value)).argmin()
    return array[idx]

def onclick(event):
    global ix,iy
    ix,iy = event.xdata, event.ydata
    #print 'x = %d, y = %d'%(ix,iy)

    global coords
    coords.append((ix,iy))

#disconnect after 2 clicks
if len(coords) ==2:
    fig.canvas.mpl_disconnect(cid)
    plt.close(1)
return

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

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

coords = []

cid = fig.canvas.mpl_connect('button_press_event', onclick)

plt.show()

#integration limits

ch1 = np.where(x == (find_nearest(x,coords[0][0])))
ch2 = np.where(x == (find_nearest(x, coords[1][0])))

#calculate integral
y_int = trapz(y[ch1[0][0]:ch2[0][0], x = x[ch1][0][0]:ch2[0][0]])

print ''
print 'Integral between ' + str(coords[0][0])+ '& ' + str(coords[1][0])
print y_int

I figured this would save coordinates and it does. It saves the final click in my terminal after closing the plot.

I wish to click multiple times on the graph and possibly save the location of these clicks to an empty array to use later. Any suggestions or different modules to import to help are great.

0 Answers0