Skip to content

[Bug]: button_press_event fails on single-click with macosx backend #30372

@cbrnr

Description

@cbrnr

Bug summary

I might have discovered a really weird bug. I've narrowed it down to three specific conditions that must all be met:

  1. macosx backend
  2. a large number of subplots (~225)
  3. the presence of at least one annotation per subplot

Code for reproduction

import matplotlib
import matplotlib.pyplot as plt

matplotlib.use("macosx")  # works with QtAgg, does not work with macosx

# with a large enough number of subplot this issue will show
N_ROWS = 15
N_COLS = 15


def on_press(event):
    if event.inaxes:
        print(
            f"Mouse click detected on '{event.inaxes.get_title()}' "
            f"(dblclick={event.dblclick})."
        )


def create_plot(ax, i):
    """Creates a plot with a single annotation."""
    ax.annotate(
        f"Ann. {i + 1}",
        xy=(0.5, 0.5),
        xycoords="axes fraction",
        ha="center",
        va="center",
        fontsize=6,
    )
    ax.set_title(f"Subplot {i + 1}", fontsize=6)
    ax.set_xticks([])
    ax.set_yticks([])


fig, axes = plt.subplots(N_ROWS, N_COLS, figsize=(12, 8))
fig.canvas.mpl_connect("button_press_event", on_press)

axes_flat = axes.flat if hasattr(axes, "flat") else [axes]
for i, ax in enumerate(axes_flat):
    create_plot(ax, i)

plt.tight_layout()
plt.show()

Actual outcome

With the macosx backend and NROWS = 10 and NCOLS = 10, single-clicks are not registered. If descreasing the number of subplots, this issue does not occur. When using the QtAgg backend, this issue does not occur either, not even for 100 subplots (or possibly higher).

Expected outcome

Single-clicks should also be registered when using the macosx backend.

Additional information

No response

Operating system

macOS

Matplotlib Version

3.10.3

Matplotlib Backend

macosx

Python version

3.13.5

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions