Hello,
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.
Thanks,
Len
#!/usr/local/bin/python
"""
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
self.run = run
if include:
self.color = self.colorin
else:
self.color = self.colorout
class LassoManager:
def __init__(self, ax, data):
self.axes = ax
self.canvas = ax.figure.canvas
self.data = 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,),
facecolors=self.facecolors,
offsets = self.xys,
transOffset = ax.transData)
ax.add_collection(self.collection)
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,self.data[i].run,self.data[i].x,self.data[i].y)
else:
self.facecolors[i] = Datum.colorout
self.canvas.draw_idle()
self.canvas.widgetlock.release(self.lasso)
del self.lasso
def onpress(self, event):
print "OK1"
print self.canvas.widgetlock.locked()
if self.canvas.widgetlock.locked():
return
print "OK2"
if event.inaxes is None:
return
print "OK3"
self.lasso = widgets.Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
# acquire a lock on the widget drawing
self.canvas.widgetlock(self.lasso)
print "OK4"
data = [Datum(*xy) for xy in rand(100, 2)]
# Fake run data
i = 0
for d in data:
d.run = i
i += 1
fig = pyplot.figure()
ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
lman = LassoManager(ax, data)
pyplot.show()
···
--
__________________________________________________
Leonard J. Reder
Jet Propulsion Laboratory
Email: reder@...369...
Phone (Voice): 818-354-3639
--------------------------------------------------