selecting points inside plot

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

selecting points inside plot

Jean-Philippe Grivet
Hello,
I use Win7 and the Anaconde suite of Python programs.
I am trying to write a piece of software to demonstrate the computation
of electric potentials. The software should ;

     - allow a user to select (with a mouse click) a point inside a
blank rectangle,
     - record the x and y coordinates of  that point
     - draw a dot at that location
     - ask the user for the value of the charge located there and record
that
     - repeat a few times
     - compute the electric potential and draw a contour plot of that
function.

There are several pieces of code available showing how to print the x,y
coor-
dinates of a mouse click but I can't devise a way to retrieve thes
values for
turther computations nor can I dra a dot at the corresponding location
or insert a dialog to get the charge values.

I would be grateful for any suggestion.
JP Grivet


---
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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

tcaswell
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

On Tue, Aug 29, 2017 at 10:29 AM Jean-Philippe Grivet <[hidden email]> wrote:
Hello,
I use Win7 and the Anaconde suite of Python programs.
I am trying to write a piece of software to demonstrate the computation
of electric potentials. The software should ;

     - allow a user to select (with a mouse click) a point inside a
blank rectangle,
     - record the x and y coordinates of  that point
     - draw a dot at that location
     - ask the user for the value of the charge located there and record
that
     - repeat a few times
     - compute the electric potential and draw a contour plot of that
function.

There are several pieces of code available showing how to print the x,y
coor-
dinates of a mouse click but I can't devise a way to retrieve thes
values for
turther computations nor can I dra a dot at the corresponding location
or insert a dialog to get the charge values.

I would be grateful for any suggestion.
JP Grivet


---
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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

Jean-Philippe Grivet
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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

Jody Klymak

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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

Benjamin Root
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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

Jody Klymak

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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
jni
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

jni
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 <[hidden email]>, 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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users



_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

Jean-Philippe Grivet
In reply to this post by Jody Klymak
Thank you Benjamin and Jody for your sugestions. Due to my scanty knwledge of Python,
I unfortunately can't get these programs to do what I want.
Referring to Jody's code, the statement "print(x)" is executed only once, upon
lauching the program. Then, the various xdata values are stored in x but nothing more
happens, until I close the graphic window. This termintes execution. If I issue print(x)
in the console, I recover the values of interest. My attempts to plot a dot where the
button was clicked have failed.
Ben's code behaves similarly, except that it starts by attempting to compute the
average of some undefined values.
Can this poor performance be due to the fact that I work inside Spyder ?
I am sorry that I keep trying your patience but I will be grateful for any
suggestion.
Sincetrely,
Jean-Philippe
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 <[hidden email]> 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)


Garanti sans virus. www.avast.com

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

Jody Klymak

OK, I think you want the equivalent of matlab’s

plot(something)
x,y = ginput(‘Click the figure’)
plot(x, y)

and you want it to execute in an interactive session in ipython (spyder)

I don’t think you can do that, though there may be some new widgets that will let it happen these days.

Historically, it is better to think of any graphical interaction as a separate program. If you are willing to do that, then plotting the data as you click is very simple:

import matplotlib
matplotlib.use('Qt5Agg')

import matplotlib.pyplot as plt
import numpy as np

class Picker(object):

    def __init__(self, ax=None):
        self.x = np.array([])
        self.y = np.array([])
        self.ax = ax
        self.dots, = ax.plot(1, 1, marker='o')
        self.dots.set_xdata(self.x)
        self.dots.set_ydata(self.y)
        self.fig = self.ax.get_figure()

    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)
        self.dots.set_xdata(self.x)
        self.dots.set_ydata(self.y)
        plt.draw()

    def get_x(self):
        return self.x

    def get_y(self):
        return self.y

fig, ax = plt.subplots(1, 1)
picker = Picker(ax=ax)
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())

You save the above as a file (mypicker.py) and in ipython you execute run mypicker.py. Note that after you have executed it, picker is still in your workspace to access.

I hope that helps.

Jody

On 5 Sep 2017, at 6:33, Jean-Philippe Grivet wrote:

Thank you Benjamin and Jody for your sugestions. Due to my scanty knwledge of Python,
I unfortunately can't get these programs to do what I want.
Referring to Jody's code, the statement "print(x)" is executed only once, upon
lauching the program. Then, the various xdata values are stored in x but nothing more
happens, until I close the graphic window. This termintes execution. If I issue print(x)
in the console, I recover the values of interest. My attempts to plot a dot where the
button was clicked have failed.
Ben's code behaves similarly, except that it starts by attempting to compute the
average of some undefined values.
Can this poor performance be due to the fact that I work inside Spyder ?
I am sorry that I keep trying your patience but I will be grateful for any
suggestion.
Sincetrely,
Jean-Philippe
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 <[hidden email]> 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)


Garanti sans virus. www.avast.com

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: selecting points inside plot

Jean-Philippe Grivet
Thank you Jody,
I'll try work my way starting with your ode.
Jean-Philippe


OK, I think you want the equivalent of matlab’s

plot(something)
x,y = ginput(‘Click the figure’)
plot(x, y)

and you want it to execute in an interactive session in ipython (spyder)

I don’t think you can do that, though there may be some new widgets that will let it happen these days.

Historically, it is better to think of any graphical interaction as a separate program. If you are willing to do that, then plotting the data as you click is very simple:

import matplotlib
matplotlib.use('Qt5Agg')

import matplotlib.pyplot as plt
import numpy as np

class Picker(object):

    def __init__(self, ax=None):
        self.x = np.array([])
        self.y = np.array([])
        self.ax = ax
        self.dots, = ax.plot(1, 1, marker='o')
        self.dots.set_xdata(self.x)
        self.dots.set_ydata(self.y)
        self.fig = self.ax.get_figure()

    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)
        self.dots.set_xdata(self.x)
        self.dots.set_ydata(self.y)
        plt.draw()

    def get_x(self):
        return self.x

    def get_y(self):
        return self.y

fig, ax = plt.subplots(1, 1)
picker = Picker(ax=ax)
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(

Garanti sans virus. www.avast.com

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users