Jupyter notebook: saving graph coordinates

Viewed 114

I have some lines of code that I use with spyder (using matplotlib) that allow me to save coordinates which are double-clicked. For some reason I don't understand the same code does not work in Jupyter Notebook.

coords = []
class onclick:
    """ Saves the coordinates of the left-double-clicked points and finishes with right-double-click """
    def __init__(self):
        self.cid = fig.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        global ix, iy
        global coords
        if event.dblclick:
            if event.button==1:
                ix, iy = event.xdata, event.ydata
                print ('Coordinate clicked: %.3f, %.3f'%(ix, iy))
                coords.append((ix, iy))
            if event.button==3:
                fig.canvas.mpl_disconnect(self.cid)
                print("Clicked points saved in 'coords'")
                return
    
cid = onclick()

Any way to make this work in Jupyter Notebook? (Don't know if this is relevant but in Jupyter Notebook I'm using %matplotlib notebook to be able to zoom in)

1 Answers

Due to a lack of response I made a workaround in which I use the mouse right-click, and not the double click, to save the coordinates. This works well in Jupyter Notebook. Here's the code:

coords = []
def onclick(event):
    global coords
    if event.button == 3:
        coords.append(event.xdata)

cid = fig.canvas.mpl_connect('button_press_event', onclick)
Related