0

I have a scatter plot with multiple markers. How would I change the code so that it can still check if I click on a point on the graph or not. What would the variable line be?

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()

1 Answer 1

1

Of course the two scatter plots have to be stored in different variables. You may then also divide your annotations into two parts, those that belong to the first scatter, and those for the second. You would then loop over them and check if the event occurs in any of them.

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()
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your answer. It works, but the behaviour is unexpected. When I click on the ones with the same marker, the annotation is visible (like is should be), but when I clicked on a different marker, the annotation of the previously selected point disappear because the points for both markers have the same index. Do you perhaps know how I can fix this?
So all annotations should stay visible independend on which marker if another annotation is clicked upon? And if a click outside a point is performed, all of them should disappear?
Yes, it should be like that. Is it possible to do it?
Yes, I updated the code to use two flags to steer the annotations to be shown or not, depending on where the click happens.
Thank you so much!! :) I have accepted and upvoted your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.