[Matplotlib-users] selecting points inside plot

Benjamin Root ben.v.root at gmail.com
Sat Sep 2 21:08:03 EDT 2017


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/20170902/f192c5f8/attachment.html>


More information about the Matplotlib-users mailing list