s.a
s.a

Reputation: 9

saving data points when picking from a plot

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)

Upvotes: 0

Views: 1023

Answers (1)

Mandias
Mandias

Reputation: 876

As mentioned in the comments, updating Python addressed the issue.

Another method which may work is use list mutability instead of global numpy arrays. This method works because the original lists still exist, and we can change their content without changing the "id" of the original list. If we were to reassign the list like with "diy_pick_x = [new items]" then we are making a new list that happens to have the same name, but does not exist outside of the onpick function.

Reviewing objects in python that are mutable may help with understanding.

import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(10, 100)
y = np.random.rand(10, 100)
# the selected data
diy_pick_x = [0] # <----- Changed
diy_pick_y = [0] # <----- Changed

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 a, b
    ind = event.ind
    print("onpick scatter:", ind, np.take(x, ind), np.take(y, ind))

    # ----------- Changed -----------
    # Loop through each point index found by the event
    for i in ind:
        # Append each point to the lists
        # Note that this changes the lists, but does not reassign them
        diy_pick_x.append(np.take(x, i))
        diy_pick_y.append(np.take(y, i))

    a = diy_pick_x
    print(diy_pick_x)
    print(diy_pick_y)

fig.canvas.mpl_connect("pick_event", onpick)
plt.show() # <----- Changed

diy_pick_x = np.array(diy_pick_x) # <----- Changed
diy_pick_y = np.array(diy_pick_y) # <----- Changed

print(diy_pick_x) # <----- Changed

Upvotes: 0

Related Questions