Skip to content

Error raised on mouse cursor hover with matplotlib>=3.10 #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
nvaytet opened this issue Apr 28, 2025 · 4 comments
Open

Error raised on mouse cursor hover with matplotlib>=3.10 #594

nvaytet opened this issue Apr 28, 2025 · 4 comments

Comments

@nvaytet
Copy link

nvaytet commented Apr 28, 2025

Describe the issue

Using matplotlib>=3.10.*, making any plot with ipympl raises an error when hovering the mouse over the axes.

Example:

import matplotlib.pyplot as plt
%matplotlib widget

fig, ax = plt.subplots()

Hovering the mouse over the plot raises

KeyError                                  Traceback (most recent call last)
File [~/miniforge3/lib/python3.12/site-packages/ipympl/backend_nbagg.py:278](http://localhost:8888/home/nvaytet/miniforge3/lib/python3.12/site-packages/ipympl/backend_nbagg.py#line=277), in Canvas._handle_message(self, object, content, buffers)
    275     self.manager.handle_json(content)
    277 else:
--> 278     self.manager.handle_json(content)

File [~/miniforge3/lib/python3.12/site-packages/matplotlib/backends/backend_webagg_core.py:474](http://localhost:8888/home/nvaytet/miniforge3/lib/python3.12/site-packages/matplotlib/backends/backend_webagg_core.py#line=473), in FigureManagerWebAgg.handle_json(self, content)
    473 def handle_json(self, content):
--> 474     self.canvas.handle_event(content)

File [~/miniforge3/lib/python3.12/site-packages/matplotlib/backends/backend_webagg_core.py:264](http://localhost:8888/home/nvaytet/miniforge3/lib/python3.12/site-packages/matplotlib/backends/backend_webagg_core.py#line=263), in FigureCanvasWebAggCore.handle_event(self, event)
    261 e_type = event['type']
    262 handler = getattr(self, f'handle_{e_type}',
    263                   self.handle_unknown_event)
--> 264 return handler(event)

File [~/miniforge3/lib/python3.12/site-packages/matplotlib/backends/backend_webagg_core.py:295](http://localhost:8888/home/nvaytet/miniforge3/lib/python3.12/site-packages/matplotlib/backends/backend_webagg_core.py#line=294), in FigureCanvasWebAggCore._handle_mouse(self, event)
    286 e_type = event['type']
    287 button = event['button'] + 1  # JS numbers off by 1 compared to mpl.
    288 buttons = {  # JS ordering different compared to mpl.
    289     button for button, mask in [
    290         (MouseButton.LEFT, 1),
    291         (MouseButton.RIGHT, 2),
    292         (MouseButton.MIDDLE, 4),
    293         (MouseButton.BACK, 8),
    294         (MouseButton.FORWARD, 16),
--> 295     ] if event['buttons'] & mask  # State *after* press[/release.](http://localhost:8888/release.)
    296 }
    297 modifiers = event['modifiers']
    298 guiEvent = event.get('guiEvent')

KeyError: 'buttons'

Note: need to set level to 'Debug' in log console to see it.

The figure is basically inactive.

Could be related to #592 but I did install 0.9.7 and it did not fix the issue.

Downgrading to matplotlib=3.9.4 fixes the issue.

Versions

 3.12.7 | packaged by conda-forge | (main, Oct  4 2024, 16:05:46) [GCC 13.3.0]
ipympl version: 0.9.7
Selected Jupyter core packages...
IPython          : 8.30.0
ipykernel        : 6.29.5
ipywidgets       : 8.1.5
jupyter_client   : 8.6.3
jupyter_core     : 5.7.2
jupyter_server   : 2.14.2
jupyterlab       : 4.3.1
nbclient         : 0.10.1
nbconvert        : 7.16.4
nbformat         : 5.10.4
notebook         : not installed
qtconsole        : not installed
traitlets        : 5.14.3
@ianthomas23
Copy link
Member

The symptoms are indeed the same as #592, and this has been fixed. I cannot reproduce with the same packages in a new environment:

ipympl                         0.9.7           pyhd8ed1ab_1          conda-forge
matplotlib                     3.10.1          py312h1f38498_0       conda-forge
Selected Jupyter core packages...
IPython          : 8.30.0
ipykernel        : 6.29.5
ipywidgets       : 8.1.5
jupyter_client   : 8.6.3
jupyter_core     : 5.7.2
jupyter_server   : 2.14.2
jupyterlab       : 4.3.1
nbclient         : 0.10.1
nbconvert        : 7.16.4
nbformat         : 5.10.4
notebook         : not installed
qtconsole        : not installed
traitlets        : 5.14.3

It looks you aren't actually using ipympl 0.9.7 at runtime, even though that version is installed.

One report of this issue at matplotlib/matplotlib#29654 was that it was a caching issue which was solved by restarting the kernel a number of times. Please can you try that?

@T-Wainwright
Copy link

T-Wainwright commented May 1, 2025

Getting the same issue with matplotlib 3.10.1, and ipympl 0.9.7, also setting the following behind the scenes to handle interactive figures:

matplotlib.use("module://ipympl.backend_nbagg")
plt.ion()

tried restarting a number of times, and reinstalling ipympl with --no-cache-dir, but no luck there.

No interactive features of interactive plots work with the error. Fixed again by downgrading matplotlib to 3.9.4 for now

@nvaytet
Copy link
Author

nvaytet commented May 1, 2025

It looks you aren't actually using ipympl 0.9.7 at runtime, even though that version is installed.

Doing

import ipympl

ipympl.__version__

in the same notebook does yield 0.9.7... 🤔

@nvaytet
Copy link
Author

nvaytet commented May 2, 2025

I cannot reproduce with the same packages in a new environment:

Can it be the Python version that is different? I can't think of anything else...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants