How to fix Lasso example bug???


What follows is the lasso example code that I am running. This is the example the comes with the matplotlib examples code. It works fine except when one clicks and does not move the mouse. It seems to hang.
I have traced it down to the self.canvas.widgetlock.locked() call within the LassoManager method called onpress. The onpress method is registered as a callback for a button press event. What I need to know is how to determine which widget has the lock and how to release the lock before returning from the callback so that everything will continue to work.

If anyone knows how to fix this please send example code.




Simple hacked test case to add fake runs and print out selected point set.

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 import widgets
import matplotlib.mlab
from matplotlib import nxutils
from matplotlib import colors
from matplotlib import collections

from matplotlib import pyplot
import numpy
from numpy.random import rand

class Datum:
     colorin = colors.colorConverter.to_rgba('red')
     colorout = colors.colorConverter.to_rgba('green')
     def __init__(self, x, y, run=None, include=False):
         self.x = x
         self.y = y = run
         if include:
             self.color = self.colorin
             self.color = self.colorout

class LassoManager:
     def __init__(self, ax, data):
         self.axes = ax
         self.canvas = ax.figure.canvas = data

         self.Nxy = len(data)

         self.facecolors = [d.color for d in data]
         self.xys = [(d.x, d.y) for d in data]
         self.facecolors[50] = (0.0,0.0,0.0,1.0)
         self.collection = collections.RegularPolyCollection(
             fig.dpi, 6, sizes=(100,),
             offsets = self.xys,
             transOffset = ax.transData)


         self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)

     def callback(self, verts):
         ind = numpy.nonzero(nxutils.points_inside_poly(self.xys, verts))[0]
         print "New selection:"
         for i in range(self.Nxy):
             if i in ind:
                 self.facecolors[i] = Datum.colorin
                 print "Index=%d, run=%d, xy = (%f,%f)" % (i,[i].run,[i].x,[i].y)
                 self.facecolors[i] = Datum.colorout

         del self.lasso

     def onpress(self, event):
         print "OK1"
         print self.canvas.widgetlock.locked()
         if self.canvas.widgetlock.locked():
         print "OK2"
         if event.inaxes is None:
         print "OK3"
         self.lasso = widgets.Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
         # acquire a lock on the widget drawing
         print "OK4"

data = [Datum(*xy) for xy in rand(100, 2)]
# Fake run data
i = 0
for d in data: = i
     i += 1

fig = pyplot.figure()
ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
lman = LassoManager(ax, data)


Leonard J. Reder
Jet Propulsion Laboratory
Email: reder@...369...
Phone (Voice): 818-354-3639