Hello darkside,

the below example seems to do the job. 
The problem with x, y = event.xdata, event.ydata could be due to a 
local-global-variable issue (solution: define x, y with keyword global, 
i.e. "global x, y" in your function 'click' [ -> they become global 
variables] to change their values in this function and use them in the main 
program, too).

best regards,
Matthias

>----------------------------------------------------------------------------------------------------------
import pylab
from matplotlib.widgets import Cursor

datax=[]
datay =[]

def click(event):
    if event.button == 1:
        if event.inaxes:
            print 'data coords: ', event.xdata, event.ydata
            datax.append(event.xdata)
            datay.append(event.ydata)
            # disconnect the connection with id 'cid' after the first run of
            # this part
            pylab.disconnect(cid)

    if event.button == 3:
        # only possible before the first run of the upper 
        # part ' if event.button == 1'
        pylab.close()

    return datax,datay

fig = pylab.figure(figsize=(8,6))
ax = fig.add_subplot(111)
ax.plot(pylab.arange(10))
cursor = Cursor(ax, useblit=False, color='red', linewidth=2)
cursor.horizOn = False

# connection with id 'cid'
cid = pylab.connect('button_press_event', click)

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

On Sunday 10 June 2007 04:54, darkside wrote:
> Hello,
> I've tried your idea, but it doesn't work.
> If I put:
> ----------------------
> datax=[]
> datay =[]
>
>
> def click(event):
>
>     if event.button == 1:
>         if event.inaxes:
>             print 'data coords: ', event.xdata, event.ydata
>             datax.append(event.xdata)
>             datay.append(event.ydata)
>             pylab.disconnect(click)
>
>     if event.button == 3:
>         pylab.close()
>
>     return datax,datay
>
> fig = pylab.figure(figsize=(8,6))
> ax = fig.add_subplot(111)
> 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()
> print 'los arrays de data son: ', datax,datay
>  ------------------------
> It returns the arrays, but it doesn't stop after one click, I have to press
> number 3 to exit.
>
> I tried to change to x, y instead of datax, datay in the following way:
>
> ------------------------
> x, y = 0,0
>
> x, y = event.x, event.y
> -----------------------------
>
> But in this case I get an error: "name 'x' is not defined"
> And I don't know why, because I only change the lines refering to datax,
> datay. It seems that only works with lists, but I don't know if this is the
> problem exactly.
>
> With the command close, I can close the figure window pressing right mouse
> button. It works (at least something works).
>
> Thank you for your help.
>
> 2007/6/4, Matthias Michler <[EMAIL PROTECTED]>:
> > 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?.
> >
> > -------------------------------------------------------------------------
> > This SF.net email is sponsored by DB2 Express
> > Download DB2 Express C - the FREE version of DB2 express and take
> > control of your XML. No limits. Just data. Click to get it now.
> > http://sourceforge.net/powerbar/db2/
> > _______________________________________________
> > Matplotlib-users mailing list
> > Matplotlib-users@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/matplotlib-users

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to