Skip to content

[Bug]: fig.canvas.mpl_connect in 3.5.2 not registering events in jupyter lab unless using widget pan or zoom controls #23470

@StevenSong

Description

@StevenSong

Bug summary

In jupyter lab, the code snippet below sets the title of the axis to the x,y coords of a mouse click, however in matplotlib 3.5.2 specifically, the clicks aren't registered unless I'm clicking around using the pan or zoom controls of the interactive widget. Downgrading to matplotlib 3.5.1, the clicks are registered with just the cursor as expected with the same ipympl version 0.9.1.

Code for reproduction

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

fig, ax = plt.subplots(figsize=(5,5))
plt.plot(np.random.randint(0, 5, 5))
plt.show()

def onclick(event):
    ax.set_title(f'x={event.x}, y={event.y}')
    print(f'x={event.x}, y={event.y}')

fig.canvas.mpl_connect('button_press_event', onclick)

Actual outcome

No axis title after clicking inside the plot with just a regular cursor
Screen Shot 2022-07-22 at 9 13 29 AM

Axis title set after clicking inside the plot with the pan tool of the interactive widget (same if using the zoom tool)
Screen Shot 2022-07-22 at 9 12 03 AM

Expected outcome

This screenshot was taken with mpl version 3.5.1 where the regular cursor click was registered and the axis title was changed
Screen Shot 2022-07-22 at 9 17 51 AM

Additional information

It's also interesting that under mpl 3.5.2, the cursor remains as the finger cursor icon after clicking while in mpl 3.5.1, the cursor icon changes back to the regular pointer - I'm not sure what that means but it is a difference in behavior...

Some relevant package versions:
matplotlib 3.5.2
ipympl 0.9.1
jupyterlab 3.4.3

Operating system

reproduced on both Ubuntu and macOS

Matplotlib Version

3.5.2

Matplotlib Backend

module://ipympl.backend_nbagg

Python version

3.8.13

Jupyter version

3.4.3

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions