我将三组数据绘制在同一张图表中。我希望使用交互式鼠标,它允许我单击图表上的点,并将这些点存储到各自的列表中。我使用此链接作为指南:使用 matplotlib 存储鼠标单击事件坐标。我可以单击图表中的点,它会显示坐标,但这些值不会存储到我定义的列表中(zradius、zangle 和 zamp)。也许我误解了链接中所做的事情?如果是这样,需要修改什么才能应用于多个列表而不是一个?
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)
输出显示如下:
In[7]: zradius
Out[7]: []
这段代码非常接近您所要求的内容,但不完全是您所要求的内容,因为我无法理解您所要求的内容,这是我的错。
请锤击它,直到它成为您需要的剑或犁
import matplotlib.pyplot as plt
import numpy as np
# some fake data, ty for not providing it
actual_x, actual_y, actual_z, ampz = np.arange(80).reshape((4,20))
radius = np.sqrt(actual_x**2 + actual_y**2)
angle = np.rad2deg(np.arctan2(actual_y,actual_x))
fig, ax = plt.subplots()
scatters = {
ax.scatter(actual_z, radius, color = 'black', marker='*', s=20, picker=True):'r',
ax.scatter(actual_z, angle, color = 'green', marker='*', s=20, picker=True):'φ',
ax.scatter(actual_z, ampz, color = 'brown', marker='x', s=20, picker=True):'z',
}
def onpick3(event, data={'r':radius, 'φ':angle, 'z':ampz}):
series = scatters[event.artist]
i = event.ind[0]
print('%s[%d] → '%(series,i), actual_z[i], data[series][i])
pick_no = fig.canvas.mpl_connect('pick_event', onpick3)
plt.show()
fig.canvas.mpl_disconnect(pick_no)
执行时会打印类似的内容
φ[4] → 44 80.53767779197439
z[2] → 42 62
r[1] → 41 21.02379604162864
我猜已经很接近了……