[Matplotlib-users] selecting points inside plot

Juan Nunez-Iglesias jni.soma at gmail.com
Mon Sep 4 04:16:25 EDT 2017


For what it’s worth, I highly recommend picking up a copy of Ben’s book. It helped me to understand not just interactive matplotlib, but interactive GUI programming in general. Thanks, Ben! ;)

On 3 Sep 2017, 6:33 PM +0300, Jody Klymak <jklymak at uvic.ca>, wrote:
> Hi Jean-Philippe,
> Just to explicitly implement Ben’s suggestion…
> Cheers, Jody
> import matplotlib
> matplotlib.use('Qt5Agg')
>
> import matplotlib.pyplot as plt
> import numpy as np
>
> class Picker(object):
>
>    def __init__(self):
>        self.x = np.array([])
>        self.y = np.array([])
>
>    def process_key(self, event):
>        print("Key:", event.key)
>
>    def process_button(self, event):
>        print("Button:", event.x, event.y, event.xdata, event.ydata, event.button)
>        self.x = np.append(self.x, event.xdata)
>        self.y = np.append(self.y, event.ydata)
>
>    def get_x(self):
>        return self.x
>
>    def get_y(self):
>        return self.y
>
> fig, ax = plt.subplots(1, 1)
> picker = Picker()
> fig.canvas.mpl_connect('key_press_event', picker.process_key)
> fig.canvas.mpl_connect('button_press_event', picker.process_button)
> plt.show()
>
> print(picker.x)  #
> print(picker.get_x())  # the same
> print(picker.get_x().mean()) # returns the mean of x.
> print(picker.get_y())
> print(picker.get_y().mean())
> On 2 Sep 2017, at 18:08, Benjamin Root wrote:
> > If you assign a class method as the callbacks, such as process_button(self, event), then that method could save the relevant values to itself. I show how to do this in my book (as well as the global approach, too).
> >
> > Cheers!
> > Ben Root
> >
> > > On Sat, Sep 2, 2017 at 10:30 AM, Jody Klymak <jklymak at uvic.ca> wrote:
> > > > Hi Jean-Philippe
> > > > There may be a fancier way, but you can just declare a global in process_button to pass the value to a global variable.
> > > > Cheers, Jody
> > > > import matplotlib.pyplot as plt
> > > >
> > > > x = []
> > > >
> > > > def process_key(event):
> > > >    print("Key:", event.key)
> > > > def process_button(event):
> > > >    global x
> > > >    print("Button:", event.x, event.y, event.xdata, event.ydata, event.button)
> > > >    x += [event.xdata]
> > > >
> > > > fig, ax = plt.subplots(1, 1)
> > > > fig.canvas.mpl_connect('key_press_event', process_key)
> > > > fig.canvas.mpl_connect('button_press_event', process_button)
> > > > plt.show()
> > > >
> > > > print(x)
> > > >
> > > > On 2 Sep 2017, at 7:01, Jean-Philippe Grivet wrote:
> > > > > Hi
> > > > > Thank you for the very interesting refeerences. However, I find that the various programs are rather
> > > > > involved and that they do not exactly answer my needs. The simplest program is probably the following
> > > > > (from the boook by B. Root). It seems to be a step in the right direction!
> > > > >
> > > > > import matplotlib.pyplot as plt
> > > > >
> > > > > def process_key(event):
> > > > > print("Key:", event.key)
> > > > > def process_button(event):
> > > > > print("Button:", event.x, event.y, event.xdata, event.ydata, event.button)
> > > > >
> > > > > fig, ax = plt.subplots(1, 1)
> > > > > fig.canvas.mpl_connect('key_press_event', process_key)
> > > > > fig.canvas.mpl_connect('button_press_event', process_button)
> > > > > plt.show()
> > > > >
> > > > > So now, how do I retrieve the values event.xdata, event.ydata s(everal times) in
> > > > > order to use them in the main program ? (By the way, this is pretty easy
> > > > > in Matlab: the function xclick the mouse coordinates and index of the button
> > > > > pressed).
> > > > > Thank you soluch for your help.
> > > > > JP Grivet
> > > > >
> > > > >
> > > > > Le 30/08/2017 04:13, Thomas Caswell a écrit :
> > > > > > Jean,
> > > > > >
> > > > > > What you want to do is totally possible!
> > > > > >
> > > > > > I suggest having a look at the examples in https://matplotlib.org/examples/widgets/index.html the `ginput` method, https://github.com/tacaswell/interactive_mpl_tutorial and Ben Root's book https://www.amazon.com/Interactive-Applications-using-Matplotlib-Benjamin/dp/1783988843
> > > > > >
> > > > > > Tom
> > > > > ---
> > > > > L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.
> > > > > https://www.avast.com/antivirus
> > > > >
> > > > > _______________________________________________
> > > > > Matplotlib-users mailing list
> > > > > Matplotlib-users at python.org
> > > > > https://mail.python.org/mailman/listinfo/matplotlib-users
> > > >
> > > > _______________________________________________
> > > > Matplotlib-users mailing list
> > > > Matplotlib-users at python.org
> > > > https://mail.python.org/mailman/listinfo/matplotlib-users
> > > >
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170904/6eacf5f2/attachment.html>


More information about the Matplotlib-users mailing list