Skip to content

Commit 8730f3a

Browse files
FIX: Handle hidden annotations during deletion in mpl plot (#13703)
1 parent da1c8fc commit 8730f3a

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

doc/changes/dev/13703.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix annotation removal logic in matplotlib based figures to correctly handle non-contiguous visible annotations, by `Johannes Herforth`_.

mne/viz/_mpl_figure.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,7 @@ def _buttonpress(self, event):
838838
# (ax_main.collections only includes *visible* annots, so we offset)
839839
visible_zorders = [span.zorder for span in spans]
840840
zorders = np.zeros_like(is_onscreen).astype(int)
841-
offset = np.where(is_onscreen)[0][0]
842-
zorders[offset : (offset + len(visible_zorders))] = visible_zorders
841+
zorders[is_onscreen] = visible_zorders
843842
# among overlapping clicked spans, prefer removing spans whose label
844843
# is the active label; then fall back to zorder as deciding factor
845844
active_clicked = was_clicked & is_active_label

mne/viz/tests/test_raw.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,24 @@ def test_plot_annotations(raw, browser_backend):
826826
not fig.mne.visible_annotations["test"] and fig.mne.visible_annotations["test2"]
827827
)
828828

829+
if ismpl:
830+
# gh-13511: hide the middle annotation to get non-contiguous is_onscreen=[T,F,T]
831+
annot = Annotations(
832+
onset=[2, 2, 2], duration=[3, 3, 3], description=["A", "B", "C"]
833+
)
834+
raw.set_annotations(annot)
835+
fig = raw.plot(start=0, duration=10)
836+
fig._fake_keypress("a")
837+
# hide "B" so is_onscreen = [T, F, T]
838+
fig.mne.show_hide_annotation_checkboxes.set_active(1)
839+
buttons = fig.mne.fig_annotation.mne.radio_ax.buttons
840+
# set active to the hidden "B" so deletion falls back to zorder
841+
buttons.set_active(1)
842+
fig._fake_click((2.5, 1.0), xform="data", button=3)
843+
# C has the highest zorder and should be deleted; the bug deleted A instead
844+
assert "C" not in raw.annotations.description
845+
assert "A" in raw.annotations.description
846+
829847

830848
@pytest.mark.parametrize("active_annot_idx", (0, 1, 2))
831849
def test_overlapping_annotation_deletion(raw, browser_backend, active_annot_idx):

0 commit comments

Comments
 (0)