<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Thank you Benjamin and Jody for your sugestions. Due to my scanty
knwledge of Python, <br>
I unfortunately can't get these programs to do what I want.<br>
Referring to Jody's code, the statement "print(x)" is executed only
once, upon <br>
lauching the program. Then, the various xdata values are stored in x
but nothing more<br>
happens, until I close the graphic window. This termintes execution.
If I issue print(x)<br>
in the console, I recover the values of interest. My attempts to
plot a dot where the<br>
button was clicked have failed.<br>
Ben's code behaves similarly, except that it starts by attempting to
compute the<br>
average of some undefined values.<br>
Can this poor performance be due to the fact that I work inside
Spyder ?<br>
I am sorry that I keep trying your patience but I will be grateful
for any <br>
suggestion.<br>
Sincetrely,<br>
Jean-Philippe <br>
<blockquote cite="mid:E73974A8-018E-4171-8B20-62FC3BFA2B75@uvic.ca"
type="cite">
<div style="font-family:sans-serif">
<div style="white-space:normal">
<pre style="border:thin solid gray; margin-left:15px; margin-right:15px; max-width:90vw; overflow-x:auto; padding:5px"><code><span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">matplotlib</span>
matplotlib<span style="color: #333333">.</span>use(<span style="background-color: #fff0f0">'Qt5Agg'</span>)
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008800; font-weight: bold">as</span> <span style="color: #0e84b5; font-weight: bold">plt</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">numpy</span> <span style="color: #008800; font-weight: bold">as</span> <span style="color: #0e84b5; font-weight: bold">np</span>
<span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">Picker</span>(<span style="color: #007020">object</span>):
<span style="color: #008800; font-weight: bold">def</span> <span style="color: #0066BB; font-weight: bold">__init__</span>(<span style="color: #007020">self</span>):
<span style="color: #007020">self</span><span style="color: #333333">.</span>x <span style="color: #333333">=</span> np<span style="color: #333333">.</span>array([])
<span style="color: #007020">self</span><span style="color: #333333">.</span>y <span style="color: #333333">=</span> np<span style="color: #333333">.</span>array([])
<span style="color: #008800; font-weight: bold">def</span> <span style="color: #0066BB; font-weight: bold">process_key</span>(<span style="color: #007020">self</span>, event):
<span style="color: #008800; font-weight: bold">print</span>(<span style="background-color: #fff0f0">"Key:"</span>, event<span style="color: #333333">.</span>key)
<span style="color: #008800; font-weight: bold">def</span> <span style="color: #0066BB; font-weight: bold">process_button</span>(<span style="color: #007020">self</span>, event):
<span style="color: #008800; font-weight: bold">print</span>(<span style="background-color: #fff0f0">"Button:"</span>, event<span style="color: #333333">.</span>x, event<span style="color: #333333">.</span>y, event<span style="color: #333333">.</span>xdata, event<span style="color: #333333">.</span>ydata, event<span style="color: #333333">.</span>button)
<span style="color: #007020">self</span><span style="color: #333333">.</span>x <span style="color: #333333">=</span> np<span style="color: #333333">.</span>append(<span style="color: #007020">self</span><span style="color: #333333">.</span>x, event<span style="color: #333333">.</span>xdata)
<span style="color: #007020">self</span><span style="color: #333333">.</span>y <span style="color: #333333">=</span> np<span style="color: #333333">.</span>append(<span style="color: #007020">self</span><span style="color: #333333">.</span>y, event<span style="color: #333333">.</span>ydata)
<span style="color: #008800; font-weight: bold">def</span> <span style="color: #0066BB; font-weight: bold">get_x</span>(<span style="color: #007020">self</span>):
<span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">self</span><span style="color: #333333">.</span>x
<span style="color: #008800; font-weight: bold">def</span> <span style="color: #0066BB; font-weight: bold">get_y</span>(<span style="color: #007020">self</span>):
<span style="color: #008800; font-weight: bold">return</span> <span style="color: #007020">self</span><span style="color: #333333">.</span>y
fig, ax <span style="color: #333333">=</span> plt<span style="color: #333333">.</span>subplots(<span style="color: #0000DD; font-weight: bold">1</span>, <span style="color: #0000DD; font-weight: bold">1</span>)
picker <span style="color: #333333">=</span> Picker()
fig<span style="color: #333333">.</span>canvas<span style="color: #333333">.</span>mpl_connect(<span style="background-color: #fff0f0">'key_press_event'</span>, picker<span style="color: #333333">.</span>process_key)
fig<span style="color: #333333">.</span>canvas<span style="color: #333333">.</span>mpl_connect(<span style="background-color: #fff0f0">'button_press_event'</span>, picker<span style="color: #333333">.</span>process_button)
plt<span style="color: #333333">.</span>show()
<span style="color: #008800; font-weight: bold">print</span>(picker<span style="color: #333333">.</span>x) <span style="color: #888888">#</span>
<span style="color: #008800; font-weight: bold">print</span>(picker<span style="color: #333333">.</span>get_x()) <span style="color: #888888"># the same</span>
<span style="color: #008800; font-weight: bold">print</span>(picker<span style="color: #333333">.</span>get_x()<span style="color: #333333">.</span>mean()) <span style="color: #888888"># returns the mean of x.</span>
<span style="color: #008800; font-weight: bold">print</span>(picker<span style="color: #333333">.</span>get_y())
<span style="color: #008800; font-weight: bold">print</span>(picker<span style="color: #333333">.</span>get_y()<span style="color: #333333">.</span>mean())
</code></pre>
<p dir="auto">On 2 Sep 2017, at 18:08, Benjamin Root wrote:</p>
</div>
<blockquote style="border-left:2px solid #5855D5; color:#5855D5;
margin:0 0 5px; padding-left:5px">
<div id="19FE66A9-2089-42B1-AF3D-228D701ED092">
<div dir="ltr">
<div>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).<br>
</div>
<div><br>
</div>
<div>Cheers!<br>
</div>
Ben Root<br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Sat, Sep 2, 2017 at 10:30 AM,
Jody Klymak <span dir="ltr"><<a
moz-do-not-send="true" href="mailto:jklymak@uvic.ca"
target="_blank"><a class="moz-txt-link-abbreviated" href="mailto:jklymak@uvic.ca">jklymak@uvic.ca</a></a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
<div style="font-family:sans-serif">
<div style="white-space:normal">
<p dir="auto">Hi Jean-Philippe</p>
<p dir="auto">There may be a fancier way, but
you can just declare a global in <code>process_button</code>
to pass the value to a global variable.</p>
<p dir="auto">Cheers, Jody</p>
<pre style="border:thin solid gray;margin-left:15px;margin-right:15px;max-width:90vw;overflow-x:auto;padding:5px"><code>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_<wbr>press_event', process_key)
fig.canvas.mpl_connect('<wbr>button_press_event', process_button)
plt.show()
print(x)
</code></pre>
</div></div></div></blockquote></div></div></div></blockquote></div></blockquote>
<div id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br /> <table style="border-top: 1px solid #D3D4DE;">
<tr>
<td style="width: 55px; padding-top: 18px;"><a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif" alt="" width="46" height="29" style="width: 46px; height: 29px;" /></a></td>
<td style="width: 470px; padding-top: 17px; color: #41424e; font-size: 13px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;">Garanti sans virus. <a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank" style="color: #4453ea;">www.avast.com</a> </td>
</tr>
</table>
<a href="#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"> </a></div></body></html>