我有一个带有多个标记的散点图。我如何更改代码,以便它仍然可以检查我是否单击图表上的某个点。变量line
会是什么?
line = ax.scatter(x[:10],y[:10],20, c=color_tag[:10], picker=True, marker='*')
# how would I change the code, if I would like to add this line?
line = ax.scatter(x[10:20],y[10:20],20, c=color_tag[10:20], picker=True, marker='^')
img_annotations = [...] #array of AnnotationBoxObjects
def show_ROI(event):
if line.contains(event)[0]:
ind = line.contains(event)[1]["ind"]
print('onpick3 scatter:', ind, np.take(d['x'], ind), np.take(d['y'], ind)
ab = img_annotations[ind[0]]
ab.set_visible(True)
else:
for ab in img_annotations:
ab.set_visible(False)
fig.canvas.draw_idle()
fig.canvas.mpl_connect('button_press_event', show_ROI)
plt.show()
答案 0 :(得分:1)
当然,两个散点图必须存储在不同的变量中。然后,您还可以将注释分为两部分,即属于第一个散布的部分和第二个散布的部分。然后,您将遍历它们并检查事件是否发生在其中任何一个中。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(20)
y = np.sin(x)
fig, ax = plt.subplots()
line1 = ax.scatter(x[:10],y[:10],20, c="red", picker=True, marker='*')
line2 = ax.scatter(x[10:20],y[10:20],20, c="green", picker=True, marker='^')
ia = lambda i: plt.annotate("Annotate {}".format(i), (x[i],y[i]), visible=False)
img_annotations = [ia(i) for i in range(len(x))]
lce = [False]
def show_ROI(event):
tlce=False
for annot, line in zip([img_annotations[:10],img_annotations[10:20]], [line1, line2]):
if line.contains(event)[0]:
lce[0]=tlce=True
ind = line.contains(event)[1]["ind"]
print('onpick3 scatter:', ind)
ab = annot[ind[0]]
ab.set_visible(True)
if not tlce:
for ab in img_annotations:
ab.set_visible(False)
lce[0] = False
fig.canvas.draw_idle()
fig.canvas.mpl_connect('button_press_event', show_ROI)
plt.show()