Can't capture Mouse and Click events from Jupyterlab 4.1.2 matplotlib interactive plot

107 views Asked by At

I tried the Jupyter notebook example for Matplotlib Event Handling and seemed to get no results.

I downloaded the jupyter notebook from https://matplotlib.org/stable/gallery/event_handling/coords_demo.html and attempted to run this code (github source here):

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.backend_bases import MouseButton

t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)


def on_move(event):
    if event.inaxes:
        print(f'data coords {event.xdata} {event.ydata},',
              f'pixel coords {event.x} {event.y}')


def on_click(event):
    if event.button is MouseButton.LEFT:
        print('disconnecting callback')
        plt.disconnect(binding_id)


binding_id = plt.connect('motion_notify_event', on_move)
plt.connect('button_press_event', on_click)

plt.show()

And all I got was a sine plot but no prints of data and pixel coordinates. Here's a screen shot of my notebook after executing the cell, clicking and moving on the image for a while, and then hitting a shift-return or two to execute the next couple cells:

enter image description here

I expected to have move or click coordinates printed or collected somewhere, but I see nothing but the plain plot.

I also tried rerunning it with a %matplotlib widget decoration on the cell, which produced the interactive plot zooming widgets, but still didn't print coordinates.

I'm using Chrome on a Mac with:

% jupyter --version
Selected Jupyter core packages...
IPython          : 8.21.0
ipykernel        : 6.29.2
ipywidgets       : 8.1.2
jupyter_client   : 8.6.0
jupyter_core     : 5.7.1
jupyter_server   : 2.12.5
jupyterlab       : 4.1.2
nbclient         : 0.8.0
nbconvert        : 7.16.1
nbformat         : 5.9.2
notebook         : not installed
qtconsole        : not installed
traitlets        : 5.14.1

With %matplotib ipympl

With this code:

%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np

from matplotlib.backend_bases import MouseButton

t = np.arange(0.0, 1.0, 0.01)
s = np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)


def on_move(event):
    if event.inaxes:
        print(f'data coords {event.xdata} {event.ydata},',
              f'pixel coords {event.x} {event.y}')


def on_click(event):
    if event.button is MouseButton.LEFT:
        print('disconnecting callback')
        plt.disconnect(binding_id)


binding_id = plt.connect('motion_notify_event', on_move)
plt.connect('button_press_event', on_click)

plt.show()

...and making sure ipympl is installed, restarting he browser, and restarting jupyter lab I have the same non-results:

After moving and clicking I still do not see the any movement coordinate or click data messages. Should they show up in the cell? somewhere?

My results are as before:

enter image description here

% conda list |grep ipympl
ipympl                    0.9.3              pyhd8ed1ab_0    conda-forge

Matplotlib issue on fixing the xample/doc raised at https://github.com/matplotlib/matplotlib/issues/27837

0

There are 0 answers