<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div name="messageBodySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;">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! ;)</div>
<div name="messageReplySection" style="font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, sans-serif;"><br />
On 3 Sep 2017, 6:33 PM +0300, Jody Klymak <jklymak@uvic.ca>, wrote:<br />
<blockquote type="cite" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #1abc9c;">
<div style="font-family:sans-serif">
<div style="white-space:normal">
<p dir="auto">Hi Jean-Philippe,</p>
<p dir="auto">Just to explicitly implement Ben’s suggestion…</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><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>
<p dir="auto"></p>
</div>
<div style="white-space:normal"></div>
<blockquote style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;">
<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 href="mailto:jklymak@uvic.ca" target="_blank">jklymak@uvic.ca</a>></span> wrote:<br />
<blockquote class="gmail_quote" style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #3498db;"><u></u>
<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 class="h5">
<p dir="auto">On 2 Sep 2017, at 7:01, Jean-Philippe Grivet wrote:</p>
<p dir="auto"></p>
</div>
</div>
</div>
<div>
<div class="h5">
<div style="white-space:normal">
<blockquote style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #d35400;">
<p dir="auto">Hi<br />
Thank you for the very interesting refeerences. However, I find that the various programs are rather<br />
involved and that they do not exactly answer my needs. The simplest program is probably the following<br />
(from the boook by B. Root). It seems to be a step in the right direction!<br />
<br />
import matplotlib.pyplot as plt<br />
<br />
def process_key(event):<br />
print("Key:", event.key)<br />
def process_button(event):<br />
print("Button:", event.x, event.y, event.xdata, event.ydata, event.button)<br />
<br />
fig, ax = plt.subplots(1, 1)<br />
fig.canvas.mpl_connect('key_<wbr />press_event', process_key)<br />
fig.canvas.mpl_connect('<wbr />button_press_event', process_button)<br />
plt.show()<br />
<br />
So now, how do I retrieve the values event.xdata, event.ydata s(everal times) in<br />
order to use them in the main program ? (By the way, this is pretty easy<br />
in Matlab: the function xclick the mouse coordinates and index of the button<br />
pressed).<br />
Thank you soluch for your help.<br />
JP Grivet<br />
<br />
<br />
Le 30/08/2017 04:13, Thomas Caswell a écrit :</p>
<blockquote style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #34495e;">
<p dir="auto">Jean,<br />
<br />
What you want to do is totally possible!<br />
<br />
I suggest having a look at the examples in <a href="https://matplotlib.org/examples/widgets/index.html" target="_blank">https://matplotlib.org/<wbr />examples/widgets/index.html</a> the `ginput` method, <a href="https://github.com/tacaswell/interactive_mpl_tutorial" target="_blank">https://github.com/tacaswell/<wbr />interactive_mpl_tutorial</a> and Ben Root's book <a href="https://www.amazon.com/Interactive-Applications-using-Matplotlib-Benjamin/dp/1783988843" target="_blank">https://www.amazon.com/<wbr />Interactive-Applications-<wbr />using-Matplotlib-Benjamin/dp/<wbr />1783988843</a><br />
<br />
Tom<br /></p>
</blockquote>
<p dir="auto">---<br />
L'absence de virus dans ce courrier électronique a été vérifiée par le logiciel antivirus Avast.<br />
<a href="https://www.avast.com/antivirus" target="_blank">https://www.avast.com/<wbr />antivirus</a><br />
<br />
______________________________<wbr />_________________<br />
Matplotlib-users mailing list<br />
<a href="mailto:Matplotlib-users@python.org" target="_blank">Matplotlib-users@python.org</a><br />
<a href="https://mail.python.org/mailman/listinfo/matplotlib-users" target="_blank">https://mail.python.org/<wbr />mailman/listinfo/matplotlib-<wbr />users</a></p>
</blockquote>
</div>
<div style="white-space:normal"></div>
</div>
</div>
</div>
</div>
<br />
______________________________<wbr />_________________<br />
Matplotlib-users mailing list<br />
<a href="mailto:Matplotlib-users@python.org">Matplotlib-users@python.org</a><br />
<a href="https://mail.python.org/mailman/listinfo/matplotlib-users" rel="noreferrer" target="_blank">https://mail.python.org/<wbr />mailman/listinfo/matplotlib-<wbr />users</a><br />
<br /></blockquote>
</div>
<br /></div>
</div>
</blockquote>
<div style="white-space:normal">
<blockquote style="margin: 5px 5px; padding-left: 10px; border-left: thin solid #e67e22;"></blockquote>
</div>
<div style="white-space:normal"></div>
</div>
</blockquote>
<div></div>
</div>
</body>
</html>