mplcursors - select point and then use point selected globally

37 Views Asked by At

I am trying to get a user to interact with a plot and select a point from the data set available on the plot. I then want the program to remember the point selected and use that point for further analysis. Meaning, i want that selected point to be a global variable. However, when I try this, the variable is still local.

selected_index = None

F_vs_D_plot = ax1.scatter(numarray[:, 0], numarray[:, 1], color='black', label="Raw Data")
Smoothed = ax1.scatter(numarray[:, 0], smoothed_data, picker=True, color='purple', label="Filtered Data")
PLOT = Smoothed

disp_top = np.array(numarray[top_of_sample_graph_idx, 0])
load_top = np.array(smoothed_data[top_of_sample_graph_idx])
ax1.scatter(disp_top, load_top, color='red',
                    label=f'Top of Sample: ({numarray[top_of_sample_graph_idx, 0]}mm, {numarray[top_of_sample_graph_idx, 1]}g)',
                    marker='x', s=150)
 ax1.legend(loc='lower center', prop=legend_font)

cursor = mplcursors.cursor(PLOT,hover=mplcursors.HoverMode.Transient)
cursor.connect("add",on_add)
# fig.canvas.mpl_connect('pick_event', on_click)

                # print(f"Selecte point index {selected_index}")
print(f"Select point index: {selected_index}")
plt.show()
0

There are 0 best solutions below