On Mon, Mar 12, 2012 at 11:05 PM, gsal <salger...@gmail.com> wrote:

>
> I am trying to change the underlying data for my plot via a pick event,
> except that the matplotlib examples for picking show a couple of functions
> with predefined signatures and I can't seem to figure out how to modify my
> data from within those functions...how to do it?
>
> Here is a minimal example of what I am trying to do:
>
> [code]
> import matplotlib.pyplot as plt
>
> def pick_rlines(art,mouseevent):
>   button = mouseevent.button
>   key = mouseevent.key
>
>   wsf.nnn = 4          # <-- how to modify wsf.nnn from here?
>   print "wsf.nnn = ", wsf.nnn
>   props = dict(nnn=wsf.nnn)
>   return True, props
>
> def pick_handler(event):
>   mouseevent = event.mouseevent
>   button = mouseevent.button
>   key = mouseevent.key
>   art = event.artist
>
>   wsf.nnn = event.nnn  # <-- or from here ?
>
> class AAA():
>   pass
>
> class BBB():
>   def __init__(self):
>      pass
>
>   wsf=AAA()            # when it is defined here?
>   wsf.nnn=0
>   GR = [1.0, 2.0, 3.0, 4.0]
>
>   fig = plt.figure()
>   ax = fig.add_axes([0.1,0.1,0.75,0.75])
>   ax.set_aspect(1)
>   rlines = ax.hlines(GR,0.0,4.0,picker=pick_rlines)
>   fig.canvas.mpl_connect('pick_event', pick_handler)
>   plt.show()
>
> if __name__ == '__main__':
>   the = BBB()



In the code as you've written it, "the" is in the global namespace, and so
your pick event handlers can see that variable.  So you could write:

  def pick_rlines(art,mouseevent):
    button = mouseevent.button
    key = mouseevent.key
    the.wsf.nnn = 4          # "the" is in the global namespace

But a cleaner design would be to make the pick handler a class method of
class "BBB", eg

class BBB:

    def __init__(self):
        self.fig = blah_blah...
        self.fig.canvas.mpl_connect('pick_event',  self.pick_handler)
        self.wsf = blah_blah...

    def pick_handler(self, artist, event):
        self.wsf.nnn = 4   # wsf is in the class namespace
------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to