1

I have a scatter plot and I want to get the initial index of the data sample when clicked on it.

fig, ax = plt.subplots()
sc = plt.scatter(features[:, 0], features[:, 1], c=labels)

On an on-click event, I want to get the index of the clicked point according to the features array. I know that I can get the coordinates of the cursor and search for the closest point in the callback but I am looking for a more direct/API-based solution.

What I tried:

def on_click(event):
    on_point, ind = sc.contains(event)
    ind = ind["ind"][0]

The problem is sc.contains(event) returns a dictionary with multiple numbers. I do not understand why this is the case given that I click only on a single point. I thought about getting the first element of the dict but not sure if this is the right solution

0

0