ginput with undefined number of points

Hi,
I trying to define an area in a pcolor plot (several plots)
using the ginput(). However since it is an irregular shape and
will be different in each plot so I cant define how many points
there will be before hand, I’ve tried the following but it
requires a double click at each point, which I would like to
avoid as it duplicates points


x = randn(10,10)
imshow(x)
button = False
points = []
while button == False:
points.append(ginput(1))
button = waitforbuttonpress()

    Anyone know a better way to go about this??

    thanks

    Dave

I would utilize the event handling mechanism within mpl to do this. You can create a function that would append the xy coordinates to points at each call. Here is a mockup.

def on_press(event) :

on_press.points.append((event.xdata, event.ydata))

on_press.points = []

Then, you would need some way to indicate that you want to start and finish, such as some sort of key press:

def key_press(event) :

if event.key == 'a' :
    if key_press.active :
        key_press.mouse_cid = key_press.fig.canvas.mpl_connect('button_press_event', on_press)
    else :
        key_press.fig.canvas.mpl_disconnect(key_press.mouse_cid)

    key_press.active = not key_press.active

key_press.active = False
key_press.fig = plt.figure()
key_press.fig.canvas.mpl_connect(‘key_press_event’, key_press)

There are some more elegant ways of doing this, particularly by creating some sort of object to hold these variables, but this is a quick and dirty way of doing it.

I hope that helps!
Ben Root

···

On Thu, Jun 7, 2012 at 9:07 AM, David Craig <dcdavemail@…287…> wrote:

Hi,
I trying to define an area in a pcolor plot (several plots)
using the ginput(). However since it is an irregular shape and
will be different in each plot so I cant define how many points
there will be before hand, I’ve tried the following but it
requires a double click at each point, which I would like to
avoid as it duplicates points


x = randn(10,10)
imshow(x)
button = False
points = []
while button == False:
points.append(ginput(1))
button = waitforbuttonpress()

    Anyone know a better way to go about this??

    thanks

    Dave

ginput has a way to do it. From docs:
        If n is zero or negative, accumulate clicks until a middle click
        (or potentially both mouse buttons at once) terminates the
        input.

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.ginput
Pressing [enter] also terminates the points acquisition.

···

Le jeudi 07 juin 2012 à 14:07 +0100, David Craig a écrit :

Hi,
I trying to define an area in a pcolor plot (several plots) using the
ginput(). However since it is an irregular shape and will be different
in each plot so I cant define how many points there will be before hand,
I've tried the following but it requires a double click at each point,
which I would like to avoid as it duplicates points

>x = randn(10,10)
imshow(x)

button = False
points = []
while button == False:
     points.append(ginput(1))
     button = waitforbuttonpress()
>

Anyone know a better way to go about this??
thanks
Dave

--
Fabrice Silva <silva@...1918...>
LMA UPR CNRS 7051