mouse cursor coordinates

Hello,
I want to retrieve the mouse cursor coordinates when i click inside a
figure. I have tried a variety ef example programs, such as

from pylab import *

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))

def onclick(event):
     print("button=%d, x=%d, y=%d, xdata=%f, ydata=%f" %(
         event.button, event.x, event.y, event.xdata, event.ydata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)
ax.set_title("click on figure")

show()

The figure gets plotted, then nothing happens: the program simply ends
and focus retruns to the IPython console.

I am using distribution Anaconda/Python35/Spyder under Win7-64. What did
I miss ?
Thank you for your help,
JP Grivet

It works well in my system (linux gentoo) with ipython-4.0.3,
python-2.7.10 and python-3.4.3.

Did you try running it outside spyder?
May be save it into a file (test_cursor.py) and run it directly from a
system console to see if the problem is with spyder.

Anyway, I am not very experienced. May be someone else may have a better
advice.
Regards,
  Juan

···

On 30/05/16 07:17, grivet wrote:

Hello,
I want to retrieve the mouse cursor coordinates when i click inside a
figure. I have tried a variety ef example programs, such as

from pylab import *

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))

def onclick(event):
    print("button=%d, x=%d, y=%d, xdata=%f, ydata=%f" %(
        event.button, event.x, event.y, event.xdata, event.ydata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)
ax.set_title("click on figure")

show()

The figure gets plotted, then nothing happens: the program simply ends
and focus retruns to the IPython console.

I am using distribution Anaconda/Python35/Spyder under Win7-64. What did
I miss ?
Thank you for your help,
JP Grivet

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Le 30/05/2016 ? 14:36, Juan a ?crit :

It works well in my system (linux gentoo) with ipython-4.0.3,
python-2.7.10 and python-3.4.3.

Did you try running it outside spyder?
May be save it into a file (test_cursor.py) and run it directly from a
system console to see if the problem is with spyder.

Anyway, I am not very experienced. May be someone else may have a better
advice.
Regards,
  Juan

Hello,
I want to retrieve the mouse cursor coordinates when i click inside a
figure. I have tried a variety ef example programs, such as

from pylab import *

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))

def onclick(event):
     print("button=%d, x=%d, y=%d, xdata=%f, ydata=%f" %(
         event.button, event.x, event.y, event.xdata, event.ydata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)
ax.set_title("click on figure")

show()

The figure gets plotted, then nothing happens: the program simply ends
and focus retruns to the IPython console.

I am using distribution Anaconda/Python35/Spyder under Win7-64. What did
I miss ?
Thank you for your help,
JP Grivet

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Juan,

It seems you are right; all the examples I had tried unsuccessfully
before work OK when launched in command

window . What should I do next ?

JP Grivet

···

On 30/05/16 07:17, grivet wrote:

Ok, I do not use spyder so I may be wrong, but it looks that it works if
in the preferences windows (Tools -> Preferences)
in the option:
              "IPython console"
In the tab:
        "Graphics"
you select the Backend:
        "Automatic"

You probably have to restart spyder for it to work.

Regards,
  Juan

···

On 30/05/16 11:40, grivet wrote:

Le 30/05/2016 ? 14:36, Juan a ?crit :

It works well in my system (linux gentoo) with ipython-4.0.3,
python-2.7.10 and python-3.4.3.

Did you try running it outside spyder?
May be save it into a file (test_cursor.py) and run it directly from a
system console to see if the problem is with spyder.

Anyway, I am not very experienced. May be someone else may have a better
advice.
Regards,
    Juan

On 30/05/16 07:17, grivet wrote:

Hello,
I want to retrieve the mouse cursor coordinates when i click inside a
figure. I have tried a variety ef example programs, such as

from pylab import *

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))

def onclick(event):
     print("button=%d, x=%d, y=%d, xdata=%f, ydata=%f" %(
         event.button, event.x, event.y, event.xdata, event.ydata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)
ax.set_title("click on figure")

show()

The figure gets plotted, then nothing happens: the program simply ends
and focus retruns to the IPython console.

I am using distribution Anaconda/Python35/Spyder under Win7-64. What did
I miss ?
Thank you for your help,
JP Grivet

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Juan,

It seems you are right; all the examples I had tried unsuccessfully
before work OK when launched in command

window . What should I do next ?

JP Grivet

Right again! A separate window is now created and I can click inside and
retrieve the pointer coordinates.
Thanks a lot,
JP Grivet

···

Ok, I do not use spyder so I may be wrong, but it looks that it works if
in the preferences windows (Tools -> Preferences)
in the option:
               "IPython console"
In the tab:
        "Graphics"
you select the Backend:
        "Automatic"

You probably have to restart spyder for it to work.

Regards,
  Juan

On 30/05/16 11:40, grivet wrote:

Le 30/05/2016 ? 14:36, Juan a ?crit :

It works well in my system (linux gentoo) with ipython-4.0.3,
python-2.7.10 and python-3.4.3.

Did you try running it outside spyder?
May be save it into a file (test_cursor.py) and run it directly from a
system console to see if the problem is with spyder.

Anyway, I am not very experienced. May be someone else may have a better
advice.
Regards,
     Juan

On 30/05/16 07:17, grivet wrote:

Hello,
I want to retrieve the mouse cursor coordinates when i click inside a
figure. I have tried a variety ef example programs, such as

from pylab import *

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))

def onclick(event):
      print("button=%d, x=%d, y=%d, xdata=%f, ydata=%f" %(
          event.button, event.x, event.y, event.xdata, event.ydata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)
ax.set_title("click on figure")

show()

The figure gets plotted, then nothing happens: the program simply ends
and focus retruns to the IPython console.

I am using distribution Anaconda/Python35/Spyder under Win7-64. What did
I miss ?
Thank you for your help,
JP Grivet

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Juan,

It seems you are right; all the examples I had tried unsuccessfully
before work OK when launched in command

window . What should I do next ?

JP Grivet

Hello,
I would like to go further and, in fact, do the following:

     repeat n times
         click inside a figure
         retrieve and store the pointer coordinates
         show a mark (or circle or whatever) at this point

Thank you for your suggestions
JP Grivet

···

Right again! A separate window is now created and I can click inside
and retrieve the pointer coordinates.
Thanks a lot,
JP Grivet

Ok, I do not use spyder so I may be wrong, but it looks that it works if
in the preferences windows (Tools -> Preferences)
in the option:
               "IPython console"
In the tab:
          "Graphics"
you select the Backend:
          "Automatic"

You probably have to restart spyder for it to work.

Regards,
    Juan

See the spline example in this gist
https://gist.github.com/tacaswell/d1afcf015fc202c311bdcc477e1ca3bd and
http://matplotlib.org/users/event_handling.html

The trick is to register an instance method into the callbacks so that you
can keep state between calls.

Tom

···

On Fri, Jun 3, 2016 at 6:12 AM grivet <grivet at cnrs-orleans.fr> wrote:

Hello,
I would like to go further and, in fact, do the following:

     repeat n times
         click inside a figure
         retrieve and store the pointer coordinates
         show a mark (or circle or whatever) at this point

Thank you for your suggestions
JP Grivet

> Right again! A separate window is now created and I can click inside
> and retrieve the pointer coordinates.
> Thanks a lot,
> JP Grivet
>
>> Ok, I do not use spyder so I may be wrong, but it looks that it works if
>> in the preferences windows (Tools -> Preferences)
>> in the option:
>> "IPython console"
>> In the tab:
>> "Graphics"
>> you select the Backend:
>> "Automatic"
>>
>> You probably have to restart spyder for it to work.
>>
>> Regards,
>> Juan

_______________________________________________
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/20160603/5d39d306/attachment.html>

I know the examples in
        http://matplotlib.org/users/event_handling.html
but they don't do what I want.
The next example is quite complex, and I don't understand much of it.

https://gist.github.com/tacaswell/d1afcf015fc202c311bdcc477e1ca3bd and
http://matplotlib.org/users/event_handling.html

I am used to programming in Scilab (or Matlab) and I wish to emulate the
following piece of code, in which I place 4
electric charges (of both signs) inside a rectangle in order to compute
later the resulting electric field.

//define plot window
a = gca(); a.tight_limits = "on";
xsetech(wrect=[0,0,1,1],frect=[-5,-5,5,5],arect = 0.99*[1,1,1,1]);

np = 4; i = 1;
xinfo(""left click in window to place a charge (4 in all)");
while i <= np
     [ib,xx,yy] = xclick(); //retrieve
button number and pointer coordinates
     if abs(xx) < 5 & abs(yy) < 5 & ib==3 //inside plot and left button
           xs(i) = xx; ys(i)=yy;
         charge(i) = evstr(x_dialog("charge value: ","1"));
         if charge(i) >0 then
             plot2d(xs(i),ys(i),style = -4,strf="002") //blue dot
         else
             plot2d(xs(i),ys(i),style = -5,strf="002") //red dot
         end
         i = i+1;
     end
     xinfo("left click in window to place a charge (" + string(5-i) + "
charge(s) remaining)");
end
.......

Is it possible to write an analogous Python code ?

Hello,
I am still trying to plot some points inside a figure using the mouse,
while storing the
points' coordinates for further use. I found the following example,
which seems to
be a step in the right direction:

import matplotlib.pyplot as plt
plt.ion()
plt.xlim(0, 10)
plt.ylim(0, 10)
mse = 0
while mse != []:
     mse = plt.ginput(timeout=10)
     if mse != []:
         print(mse)
         plt.gca().add_artist(plt.Circle(mse[0], 0.1, alpha=0.9))
plt.close()

However, when I run this program (Spyder, Python 3.5, Win_7_64), I have
to click twice
inside the figure to draw a single point.
Any suggestion ?

JP Grivet