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, GitHub - matplotlib/interactive_tutorial: Interactive Matplotlib tutorial and Ben Root's book Amazon.com
> > > > >
> > > > > Tom
> > > > ---
> > > > L'absence de virus dans ce courrier ?lectronique a ?t? v?rifi?e par le logiciel antivirus Avast.
> > > > Download Free Antivirus Software | Avast 2024 PC Protection
> > > >
> > > > _______________________________________________
> > > > Matplotlib-users mailing list
> > > > Matplotlib-users at python.org
> > > > Matplotlib-users Info Page
> > >
> > > _______________________________________________
> > > Matplotlib-users mailing list
> > > Matplotlib-users at python.org
> > > Matplotlib-users Info Page
> > >
>