I want to select some points from a plot. I am using event picking and I want to store the data points that I select to numpy array. There is no problem about selecting points. I will select them and I can see those points in output (by printing command in function). The problem is that I don't know how to save these selected points, so that I can use them later. I am using global variable, but noting is saved to them after selecting data points when I print them independently. These data points are added to an array for each click (I know this from print command in each function calling), but it seems that when I print the array myself, they are empty. Any help appreciated!
import numpy as np
import matplotlib.pyplot as plt
x= np.random.rand(10, 100)
y = np.random.rand(10, 100)
# the selected data
diy_pick_x = np.zeros(0)
diy_pick_y = np.zeros(0)
a= np.zeros(0)
b = np.zeros(0)
fig, ax = plt.subplots()
col = ax.scatter(x, y, 0.3, picker=True)
plt.xlabel('Wavelength '+r'[${\rm \AA}$]')
plt.ylabel('Flux '+r'[${\rm 10^{-17} erg s^{-1} \AA^{-1}}$]')
def onpick(event):
global diy_pick_x, diy_pick_y
global a, b
ind = event.ind
print("onpick scatter:", ind, np.take(x, ind), np.take(y, ind))
diy_pick_x = np.append(diy_pick_x, np.take(x, ind))
diy_pick_y = np.append(diy_pick_y, np.take(y, ind))
a=diy_pick_x
print(diy_pick_x)
print(diy_pick_y)
fig.canvas.mpl_connect('pick_event', onpick)
When I print the array, this is the result:
print(diy_pick_x)
Out[6]: array([], dtype=float64)