'Is there a way to pick a point on a matplotlib scatterplot without stopping the code from executing?

I'm trying to pick a point on a matplotlib scatterplot using "pick_event", set the value of a variable to the "x" coordinate of that point, and continue executing the code to work with that variable.

Here's the working code I have so far:


%matplotlib notebook
import matplotlib.pyplot as plt

x = np.random.rand(10)
y = np.random.rand(10)
def on_pick(event):
    offsets = event.artist.get_offsets()[event.ind][0]
    global x1 
    if x1 == "null":  
        x1 = offsets[0]
        title = "X1 " + str(x1)
    plt.title(title)
    
x1 = "null"
plt.scatter(x,y,picker = 5)
fig = plt.gcf()
cid1 = fig.canvas.mpl_connect("pick_event", on_pick)
fig.canvas.draw()

This code works to select the x value, but I can't do anything with it besides changing things about the plot, such as the title. I would like to add something akin to the following to the code:

while x1 == "null":
    time.sleep(.1)#keeping the code running while I pick the x1 value, I expected that picking x1 would break me out of the loop
x2 = x1**2 #Example calculation with x1 taken from the picker

However, I can no longer pick x1 from the plot when I add this section. Does anyone have any ideas on how I could pick x1 from the plot without stopping the code from executing?

I've tried to subvert the problem by generating the plot on another thread, but I had no success. This was my first time trying to use threading, and I'm not sure that would solve the problem anyway.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source