···
-----
from matplotlib.widgets import Lasso
from matplotlib.nxutils import points_inside_poly
from matplotlib.colors import colorConverter
from matplotlib.collections import RegularPolyCollection
from matplotlib.pyplot import figure, show
from numpy import nonzero
from numpy.random import rand
from scipy import linspace, polyval, polyfit, sqrt, stats, randn
class Datum:
colorin = colorConverter.to_rgba('red')
colorout = colorConverter.to_rgba('green')
def __init__(self, x, y, include=False):
self.x = x
self.y = y
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)
facecolors = [d.color for d in data]
self.xys = [(d.x, d.y) for d in data]
fig = ax.figure
self.collection = RegularPolyCollection(
fig.dpi, 6, sizes=(100,),
facecolors=facecolors,
offsets = self.xys,
transOffset = ax.transData)
ax.add_collection(self.collection)
self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
self.ind = None
def callback(self, verts):
if hasattr(self, 'temp'):
self.temp.pop(0).remove()
facecolors = self.collection.get_facecolors()
ind = nonzero(points_inside_poly(self.xys, verts))[0]
selected =
for i in range(self.Nxy):
if i in ind:
facecolors[i] = Datum.colorin
selected.append(self.xys[i])
else:
facecolors[i] = Datum.colorout
xs =
ys =
for n in selected:
xs.append(n[0])
ys.append(n[1])
(ar,br) = polyfit(xs,ys,1)
t = linspace(min(xs),max(xs))
print('ar=%s br=%s' % (ar,br))
xr = polyval([ar,br],t)
self.temp = self.axes.plot(t,xr,'-',c='b')
self.canvas.draw_idle()
self.canvas.widgetlock.release(self.lasso)
del self.lasso
self.ind = ind
def onpress(self, event):
print('clicked')
if self.canvas.widgetlock.locked(): return
if event.inaxes is None: return
self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata),
self.callback)
# acquire a lock on the widget drawing
# self.canvas.widgetlock(self.lasso)
class start_lasso():
def __init__(self):
data = [Datum(*xy) for xy in rand(100, 2)]
fig = figure()
ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
lman = LassoManager(ax, data)
show()
--------------------------------
call_lasso.py
----
import wx
import lasso_test
class MyFrame(wx.Frame):
""" We simply derive a new class of Frame. """
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
button = wx.Button(self, label='Open Lasso')
self.Bind(wx.EVT_BUTTON, self.open_lasso, button)
self.Show(True)
def open_lasso(self, event):
lasso_test.start_lasso()
app = wx.App(False)
frame = MyFrame(None, 'Small editor')
app.MainLoop()
-----------------------------
Thanks,
Mark
On Wed, Sep 5, 2012 at 5:15 PM, Tony Yu <tsyu80@...287...> wrote:
On Wed, Sep 5, 2012 at 7:19 PM, Mark Budde <markbudde@...287...> wrote:
Hi,
I'm not an expert so please go easy on me. I am using the pyplot lasso
demo, and have got it to work how I would like. I am having a problem,
however, where I cannot get it to work if my python file is not the
main file (where __name__ is not __main__).
I took the part at the bottom in the "if __name__ == '__main__':" and
put it into a class. Everything works fine if I call the class from
within the same file. However, if I call the class from another file,
the graph loads fine but the lasso tool does not work. Troubleshooting
revealed that LassoManager.onpress is not being called when I click
the mouse. Any suggestions are welcome because this is driving me
crazy!
Thanks,
Mark
Hi Mark,
I can't seem to reproduce your issue, but it's a bit difficult without
seeing how exactly you wrapped up the "main" part of the code. Just
guessing: maybe the two cases aren't *exactly* the same.
Is it possible that you have ``lman`` (the LassoManager instance) defined in
the same block of code as ``show`` in one case but not the other? If, for
example, ``lman`` is defined in a method of your class, but not saved
anywhere then it'll get discarded after the method finishes. So ``show``
would need to be called inside that method, or saved as a class attribute.
Like I said, that's just a wild guess. You should paste the class def if
you're still having problems.
Best
-Tony
P.S. If you're running matplotlib from Github master, you might be
interested in an alternative lasso tool (LassoSelector) that may be simpler
to use.:
https://github.com/matplotlib/matplotlib/blob/master/examples/widgets/lasso_selector_demo.py