mouse events, get data and disconnect

I’m trying to use matplotlib to get an x point on some figures by mouse
clicking.
To do this I try the next example:

···
datax=[]
datay =[]
def click(event):

    x, y = event.x, event.y
    if event.button == 1:
        if event.inaxes:
            print 'data coords: ', event.xdata, event.ydata
            datax.append(event.xdata)
            datay.append(event.ydata)
    if event.button == 3:
        pylab.close()
    return datax,datay
fig = pylab.figure(figsize=(8,6))
ax = fig.add_subplot(111)
  • Ignored:

    ax.plot(pylab.arange(10))
    cursor = Cursor(ax, useblit=False, color=‘red’, linewidth=2)
    cursor.horizOn = False

    pylab.connect(‘button_press_event’, click)

    pylab.show
    ()
    pylab.disconnect(click)
    print 'los arrays de data son: ', datax,datay

    My problem is tha I want to only be able to click each figure once, then

    lock the
    figure (making it unclickable), close it and open another one. But I’m not
    able to
    implement this.
    Anyone have any suggestions?.

Hello darkside,

maybe the following little examples helps you with disconnecting:

--------------------------------------------------------------------------------

from pylab import *

def event_response(event):
    print event.name
    disconnect(cid)
  
subplot(111)
cid = connect('button_press_event', event_response)

show()

-----------------------------------------------------------------------------

concerning opening of figures:
I think it is not possible to open a figure after the show()
( I asked some time ago with subject "open / close or set active figures
during mainloop", but unfortunately without response).
  
Maybe it would be sufficient for you to clean the figure or the axes
(pylab.clf/cla), resetting labels and use the same connected event several
times.

best regards,
Matthias

···

On Monday 04 June 2007 04:28, darkside wrote:

  I'm trying to use matplotlib to get an x point on some figures by mouse
   clicking.
   To do this I try the next example:
   -------------------------
   datax=[]
   datay =[]
   def click(event):
       x, y = event.x, event.y
       if event.button == 1:
           if event.inaxes:
               print 'data coords: ', event.xdata, event.ydata
               datax.append(event.xdata)
               datay.append(event.ydata)
       if event.button == 3:
           pylab.close()
       return datax,datay
   fig = pylab.figure(figsize=(8,6))
   ax = fig.add_subplot(111)

- Ignored:
   ax.plot(pylab.arange(10))
   cursor = Cursor(ax, useblit=False, color='red', linewidth=2)
   cursor.horizOn = False

   pylab.connect('button_press_event', click)

   pylab.show()
   pylab.disconnect(click)
   print 'los arrays de data son: ', datax,datay
   ------------------------------------------------------

   My problem is tha I want to only be able to click each figure once, then
   lock the
   figure (making it unclickable), close it and open another one. But I'm
not
   able to
   implement this.
   Anyone have any suggestions?.