Issue 1:
Bug report

Pick events are fired twice instead of once following #16220. This example is a bit wacky, but it's the minimal version of a much more complete GUI:

# -*- coding: utf-8 -*-

import numpy as np
from matplotlib.figure import Figure
from matplotlib.text import Text
import matplotlib # noqa
from matplotlib.pyplot import figure
from matplotlib.widgets import Button
from mpl_toolkits.axes_grid1.axes_size import Fixed
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
# matplotlib.use('Agg', force=True)

class Figure1(Figure):
"""Interactive figure with scrollbars, for data browsing."""

def __init__(self, yticks, **kwargs):
super().__init__(figsize=(6, 6))
ax_main = self.add_subplot(1, 1, 1, position=[0.1, 0.1, 0.9, 0.9])
div = make_axes_locatable(ax_main)
ax_button = div.append_axes(position='bottom', size=Fixed(0.5), pad=0)
self._button = Button(ax_button, 'Help')

# main plot
offsets = np.arange(len(yticks))
ax_main.set(xlim=[0, 1], yticks=offsets)
ax_main.set_yticklabels(yticks, picker=True)
ax_main.plot([0, 1], [offsets, offsets])

picked = list()

def on_pick(event):
"""Handle matplotlib pick events."""
if isinstance(event.artist, Text):
ch_name = event.artist.get_text()
print(f'pick {ch_name}')
picked.append(ch_name)

yticks = list(x * 5 for x in 'ABCD')
fig = figure(FigureClass=Figure1, yticks=yticks)
fig.canvas.mpl_connect('pick_event', on_pick)
fig.canvas.draw()

ax = fig.axes[0]
x, y = ax.transData.transform_point((-0.1, 0))
func = fig.canvas.button_press_event(x=x, y=y, button=1, guiEvent=None)
assert 'AAAAA' in picked
assert len(picked) == 1, len(picked)

If you click on a ylabel (or simulate this with the code above), you get two pick events instead of one.

Actual outcome

On 6d79e6e / after #16220:

pick AAAAA
pick AAAAA
Traceback (most recent call last):
File "/home/larsoner/Desktop/fig.py", line 52, in <module>
assert len(picked) == 1, len(picked)
AssertionError: 2

Expected outcome

On 74d6145 / before #16220:

pick AAAAA

Matplotlib version

Matplotlib version (import matplotlib; print(matplotlib.__version__)): 6d79e6e or later

Matplotlib backend (print(matplotlib.get_backend())): Tested on Qt5Agg and Agg

Python version: 3.9.0

cc @anntzer since it was your PR that seems to have caused this.

Issue 2:
Bug report

duplicate pick events occurring in 3.4.rc1

In MNE-Python, our CIs that test against 3.4.rc1 are failing, because pick events are getting fired twice for every time an artist is picked (this is not happening on 3.3.4). It's happening in a very complicated interactive figure, and I haven't yet managed to work up an MWE that doesn't depend on MNE-Python. But, I've used git bisect to isolate the first test failure to matplotlib commit 6d79e6e. I will continue trying to further isolate this, but in the meantime if anyone is familiar with MPL changes in the last 4 months, I'd be happy to hear ideas about what might be going on here.

Code for reproduction

This code mirrors what we're doing but is too minimal, in that it doesn't yield doubled pick events:

import matplotlib.pyplot as plt
from matplotlib.text import Text

def pick_handler(event):
if isinstance(event.artist, Text):
print("PICKED")

fig, ax = plt.subplots()
_ = ax.plot((0, 1), (0, 1))
for lab in ax.get_yticklabels():
lab.set_picker(True)

fig.canvas.mpl_connect('pick_event', pick_handler)

This code reproduces, but with mne dependency:

import numpy as np
import matplotlib.pyplot as plt
import mne
# make fake data
data = np.random.standard_normal((2, 100))
info = mne.create_info(['ch1', 'ch2'], 1000)
raw = mne.io.RawArray(data, info)
# plot it
fig = raw.plot()

In the resulting figure, clicking on one of the yticklabels (the channel names) is supposed to mark the channel as bad (which among other effects, will turn it light grey). This doesn't happen because the pick event is fired twice, effectively marking and then unmarking the bad channel before a redraw can happen (I have confirmed this by simply inserting a print statement in the pick handler; it prints twice for each click). There is nothing wrong with the code that marks bad channels; you can do it by clicking on the channel trace itself (which responds to button_press_event instead of pick_event, for complicated reasons).

Matplotlib version

Operating system:

Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.3.2.post1516+g6d79e6edd

Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg

Python version: 3.8.8

Jupyter version (if applicable):

Other libraries: mne: current main (0.23.dev0)

matplotlib installed via pip install -e . from a git clone.