Hi all
I am new to the interactive matplotlib scripting. I need to get coordinate list of some pixels interactively from the image.
My aim is to get the coordinate list of some region first by selecting interactively. If I could select the region successfully, then using a key press (say 'a' key) save that list. If the selection is bad then I want to ignore the list using a key press (say, 'c' key) and go for the next selection. When I finish the selection quit the interactive mode by using another key press (say, 'q').
I tried to modify the lasso_demo.py but failed to achieve the task. I am attaching the code. Could anybody help me?
Thanks
Vinu V

#!/usr/bin/env python
"""
Show how to use a lasso to select a set of points and get the indices
of the selected points.  A callback is used to change the color of the
selected points

This is currently a proof-of-concept implementation (though it is
usable as is).  There will be some refinement of the API and the
inside polygon detection routine.
"""
from matplotlib.widgets import Lasso
import matplotlib.mlab
from matplotlib.nxutils import points_inside_poly
from matplotlib.colors import colorConverter
from matplotlib.collections import RegularPolyCollection
import pyfits
import pylab
from pylab import figure, show, nx
import numpy as n
from matplotlib.patches import Polygon

class LassoManager:
    def __init__(self, ax, x, y, z):
        self.axes = ax
        self.canvas = ax.figure.canvas
        self.x = x
        self.y = y
        self.z = z
        ax.contourf(x,y,z, 50)
        x1 = n.reshape(x, (size * size, 1))
        y1 = n.reshape(y, (size * size, 1))
        self.xy = n.concatenate((x1,y1),axis=1)
        self.Nxy = size * size
        self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
        self.cid2 = fig.canvas.mpl_connect('key_press_event', self.onpress1)
    def onpress1(self, event):
        if event.key=='a':
            accept = 'yes'
            print accept
        if event.key=='c':
            accept = 'no'
            print accept
        elif event.key=='q':
            pylab.close()
    def callback(self, verts):
        ind = nx.nonzero(points_inside_poly(self.xy, verts))
        print ind % size, ind / size
        for i in range(self.Nxy):
            if i in ind:
                pass
            else:
                pass
        rect = Polygon(verts, facecolor='red', alpha=0.3)
        self.axes.add_patch(rect)
        self.canvas.draw_idle()
    def onpress(self, event):
        self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)


f = pyfits.open('I_EDCSNJ1216453-1201176.fits')
z = f[0].data
f.close()
size = z.shape[0]
x = n.reshape(n.arange(size * size), (size, size)) % size
x = x.astype(n.float32)
y = n.reshape(n.arange(size * size), (size, size)) / size
y = y.astype(n.float32)
fig = figure()
ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
lman = LassoManager(ax, x, y, z)
show()


--
VINU VIKRAM
http://iucaa.ernet.in/~vvinuv/