Modifying lasso_demo.py

Hello, I'm trying to incorporate some matplotlib widgets into my scripts that generate plots. I'm unsure why, but if I wrap the figure creation lines in lasso_demo.py with a function the demo does not work. Why?

#Original:

···

#------------
if __name__ == '__main__':
     data = [Datum(*xy) for xy in randn(100,2)]
     fig = figure()
     ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
     lman = LassoManager(ax, data)
     show()

#Change:
#------------
def run(points):
     data = [Datum(*xy) for xy in points]
     fig = figure()
     ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
     lman = LassoManager(ax, data)
     show()

if __name__ == '__main__':
     points = rand(100, 2)
     run(points)

--
---------------
Scott T. Henderson
http://www.geo.cornell.edu/eas/gstudent/sth54/contact.html

Your changes work fine on my system.

Are you sure the example you wrote above is exactly the same as what you’re running? For example, if I move the show out of the function and back into ‘main’, then the lasso stops working (because the lasso manager gets garbage collected). Otherwise, I can’t reproduce the issue. Actually, what did you mean by “does not work” (e.g., no lasso, error,)?

-Tony

···

On Wed, Jul 25, 2012 at 5:11 PM, Scott Henderson <sth54@…163…> wrote:

Hello, I’m trying to incorporate some matplotlib widgets into my scripts

that generate plots. I’m unsure why, but if I wrap the figure creation

lines in lasso_demo.py with a function the demo does not work. Why?

#Original:

#------------

if name == ‘main’:

 data = [Datum(*xy) for xy in randn(100,2)]

 fig = figure()

 ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)

 lman = LassoManager(ax, data)

 show()

#Change:

#------------

def run(points):

 data = [Datum(*xy) for xy in points]

 fig = figure()

 ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)

 lman = LassoManager(ax, data)

 show()

if name == ‘main’:

 points = rand(100, 2)

 run(points)


Scott T. Henderson

http://www.geo.cornell.edu/eas/gstudent/sth54/contact.html

Thanks for the response. To be more specific, if I run the modified script in an ipython --pylab terminal:

In [1]: %run lasso_demo.py

In [2]:

The function returns, and the lasso manager is garbage collected as you say. In other words, the plot shows up, but I can't draw a lasso and select points. If I run the script in ipython without the pylab flag it works!

Why does enabling --pylab have this effect? Since I use pylab for day-to-day computing I'd like to incorporate some of these widgit tools in interactive sessions, but maybe that's not possible?

Thanks,
Scott

···

On Jul 25, 2012, at 5:10 PM, Tony Yu wrote:

def run(points):
     data = [Datum(*xy) for xy in points]
     fig = figure()
     ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
     lman = LassoManager(ax, data)
     show()

if __name__ == '__main__':
     points = rand(100, 2)
     run(points)

Thanks for the response. To be more specific, if I run the modified
script in an ipython --pylab terminal:

In [1]: %run lasso_demo.py

In [2]:

The function returns, and the lasso manager is garbage collected as
you say. In other words, the plot shows up, but I can't draw a lasso
and select points. If I run the script in ipython without the pylab
flag it works!

Why does enabling --pylab have this effect? Since I use pylab for
day-to-day computing I'd like to incorporate some of these widgit
tools in interactive sessions, but maybe that's not possible?

It is entirely possible.

--pylab mode turns mpl interactive mode on, so that the call to show() does not block. But because you have that call inside your function, if show() doesn't block, you immediately return from the function, and as Tony noted, your widget gets garbage-collected.

Try this: keep references to your figure and LassoManager in a list, dictionary, class, or whatever you like, and return that from your run() function. When you call run, assign its output to a name, so there is a reference to it, and indirectly to the LassoManager.

Eric

···

On 2012/07/25 12:45 PM, Scott Henderson wrote:

Thanks,
Scott

On Jul 25, 2012, at 5:10 PM, Tony Yu wrote:

def run(points):
      data = [Datum(*xy) for xy in points]
      fig = figure()
      ax = fig.add_subplot(111, xlim=(0,1), ylim=(0,1), autoscale_on=False)
      lman = LassoManager(ax, data)
      show()

if __name__ == '__main__':
      points = rand(100, 2)
      run(points)

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options