0

I have three sets of data that I plotted together in the same graph. I'm trying to use the mpldatacursor library so that I can click on the points and it stores the values into separate lists that correspond to the data set. I used this link as a guide: Store mouse click event coordinates with matplotlib. I am able to click on the points in the graph and it shows the coordinates, but the values are not storing into the list that I defined (zradius, zangle, and zamp). In the program, I clearly state to append the values that are clicked on, but no values are being stored. Any suggestions as to why the lists are coming up empty?

fig1, ax = plt.subplots()
fig1 = plt.figure(figsize= (10,10))
radius  = np.sqrt(actual_x**2 + actual_y**2)
angle = np.arctan2(actual_y,actual_x)
angle = angle/0.01745
amplitudez = amp0/10.
#plt.scatter(actual_z,actual_x, color = 'r', label = 'Experiment Trajectory(x_z)',marker = 
             '*',s = 20)

for i in range(len(actual_x)):
    ax.scatter([actual_z],[radius], color = 'black', 
               label='ID: Z: {:.2f},Radius: {:.2f}'.format(actual_z[i],radius[i]),marker = 
               '*',s = 20)
    ax.scatter([actual_z],[angle], color = 'green', 
               label='ID: Z: {:.2f},Angle: {:.2f}'.format(actual_z[i],angle[i]),marker = '*',s 
               = 20)
    ax.scatter([actual_z],[amplitudez], color = 'brown', 
               label='ID: Z: {:.2f},AmpZ: {:.2f}'.format(actual_z[i],amplitudez[i]),marker = 
               'x',s = 20)

ax.set_title('Experimental Data event:')
ax.set_xlabel('Z values')
ax.set_ylabel('Y/X values')
ax.set_xlim(100,700)
# # plt.ylim(-150,150)
#ax.legend()
ax.grid()
#print(' event number:',selected_event)
#print('Experimental Trajectory:','Theta:', exp_theta, 'Phi:', exp_phi, 'Energy:', exp_energy 
      )
datacursor(formatter='{label}'.format, bbox=dict(fc='yellow', alpha =10.0, snap =True))
zradius, zangle, zamp = [], [], []

def onclick(event):
    global iz, iradius, iangle, iamplitudez
    iz, iradius, iangle, iamplitudez = event.zdata, event.rdata, event.adata, event.ampdata
#     print ('x = %d, y = %d'%(
#         ix, iy))

    global zradius, zangle, zamp
    zradius.append((iz, iradius))
    zangle.append((iz, iangle))
    zamp.append((iz, iamplitudez))

    if len(zradius) == len(actual_x):
        fig.canvas.mpl_disconnect(cid)

    return zradius, zangle, zamp
cid = fig.canvas.mpl_connect('button_press_event', onclick)

The output is showing this:

In[7]: zradius
Out[7]: []
theheretic
  • 57
  • 6
  • Maybe the `onclick` function isn't called? – mkrieger1 Mar 04 '23 at 17:01
  • I edited the question with a link that I used as a guide. In the link, the onclick function isn't being called. I also tested the program myself and ran it and it seems to be working. The list in the link appends the values clicked in the graph as expected. – theheretic Mar 04 '23 at 17:44

0 Answers0