Skip to content

Commit 1ebb93e

Browse files
authored
FIX: do not cache canvas object (mne-tools#13606)
1 parent db089c9 commit 1ebb93e

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

doc/changes/dev/13606.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where :func:`mne.viz.plot_raw` would access incorrect matplotlib attributes, by :newcontrib:`Thomas Caswell`.

doc/changes/names.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@
326326
.. _Teon Brooks: https://github.com/teonbrooks
327327
.. _Tharupahan Jayawardana: https://github.com/tharu-jwd
328328
.. _Thomas Binns: https://github.com/tsbinns
329+
.. _Thomas Caswell: https://tacaswell.github.io
329330
.. _Thomas Hartmann: https://github.com/thht
330331
.. _Thomas Radman: https://github.com/tradman
331332
.. _Théodore Papadopoulo: https://github.com/papadop

mne/viz/tests/test_raw.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,13 @@ def test_scale_bar(browser_backend):
318318
assert_allclose(y_lims, bar_lims, atol=1e-4)
319319

320320

321-
def test_plot_raw_selection(raw, browser_backend, monkeypatch):
321+
def test_plot_raw_selection(raw, browser_backend):
322322
"""Test selection mode of plot_raw()."""
323323
ismpl = browser_backend.name == "matplotlib"
324324
with raw.info._unlock():
325325
raw.info["lowpass"] = 10.0 # allow heavy decim during plotting
326326
browser_backend._close_all() # ensure all are closed
327327
assert browser_backend._get_n_figs() == 0
328-
# https://github.com/matplotlib/matplotlib/issues/30575
329-
monkeypatch.setattr(mne.viz.utils, "_BLIT_KWARGS", dict(useblit=False))
330-
monkeypatch.setattr(mne.viz._mpl_figure, "_BLIT_KWARGS", dict(useblit=False))
331328
fig = raw.plot(group_by="selection", proj=False)
332329
assert browser_backend._get_n_figs() == 2
333330
sel_fig = fig.mne.fig_selection
@@ -496,13 +493,10 @@ def test_plot_raw_child_figures(raw, browser_backend):
496493
fig._resize_by_factor(0.5)
497494

498495

499-
def test_orphaned_annot_fig(raw, browser_backend, monkeypatch):
496+
def test_orphaned_annot_fig(raw, browser_backend):
500497
"""Test that annotation window is not orphaned (GH #10454)."""
501498
if browser_backend.name != "matplotlib":
502499
return
503-
# https://github.com/matplotlib/matplotlib/issues/30575
504-
monkeypatch.setattr(mne.viz.utils, "_BLIT_KWARGS", dict(useblit=False))
505-
monkeypatch.setattr(mne.viz._mpl_figure, "_BLIT_KWARGS", dict(useblit=False))
506500
assert browser_backend._get_n_figs() == 0
507501
fig = raw.plot()
508502
_spawn_child_fig(fig, "fig_annotation", browser_backend, "a")
@@ -527,7 +521,7 @@ def _norm():
527521
fig.showNormal = _norm
528522

529523

530-
def test_plot_raw_keypresses(raw, browser_backend, monkeypatch):
524+
def test_plot_raw_keypresses(raw, browser_backend):
531525
"""Test keypress interactivity of plot_raw()."""
532526
with raw.info._unlock():
533527
raw.info["lowpass"] = 10.0 # allow heavy decim during plotting

mne/viz/utils.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,6 @@ def __init__(
16561656
from matplotlib.widgets import LassoSelector
16571657

16581658
self.fig = ax.figure
1659-
self.canvas = ax.figure.canvas
16601659
self.collection = collection
16611660
self.names = names
16621661
self.alpha_selected = alpha_selected
@@ -1722,14 +1721,14 @@ def on_select(self, verts):
17221721

17231722
# Don't respond to single clicks without extra keys being hold down.
17241723
# Figures like plot_evoked_topo want to do something else with them.
1725-
if len(verts) <= 3 and self.canvas._key not in ["control", "ctrl+shift"]:
1724+
if len(verts) <= 3 and self.fig.canvas._key not in ["control", "ctrl+shift"]:
17261725
return
17271726

17281727
path = Path(verts)
17291728
inds = np.nonzero([path.intersects_path(p) for p in self.paths])[0]
1730-
if self.canvas._key == "control": # Appending selection.
1729+
if self.fig.canvas._key == "control": # Appending selection.
17311730
self.selection_inds = np.union1d(self.selection_inds, inds).astype("int")
1732-
elif self.canvas._key == "ctrl+shift":
1731+
elif self.fig.canvas._key == "ctrl+shift":
17331732
self.selection_inds = np.setdiff1d(self.selection_inds, inds).astype("int")
17341733
else:
17351734
self.selection_inds = inds
@@ -1739,9 +1738,9 @@ def on_select(self, verts):
17391738

17401739
def select_one(self, ind):
17411740
"""Select or deselect one sensor."""
1742-
if self.canvas._key == "control":
1741+
if self.fig.canvas._key == "control":
17431742
self.selection_inds = np.union1d(self.selection_inds, [ind])
1744-
elif self.canvas._key == "ctrl+shift":
1743+
elif self.fig.canvas._key == "ctrl+shift":
17451744
self.selection_inds = np.setdiff1d(self.selection_inds, [ind])
17461745
else:
17471746
return # don't notify()
@@ -1768,7 +1767,7 @@ def style_objects(self):
17681767
self.collection.set_facecolors(self.fc)
17691768
self.collection.set_edgecolors(self.ec)
17701769
self.collection.set_linewidths(self.lw)
1771-
self.canvas.draw_idle()
1770+
self.fig.canvas.draw_idle()
17721771

17731772
def disconnect(self):
17741773
"""Disconnect the lasso selector."""
@@ -1777,7 +1776,7 @@ def disconnect(self):
17771776
self.ec[:, -1] = self.alpha_selected
17781777
self.collection.set_facecolors(self.fc)
17791778
self.collection.set_edgecolors(self.ec)
1780-
self.canvas.draw_idle()
1779+
self.fig.canvas.draw_idle()
17811780

17821781

17831782
def _get_color_list(*, remove=None):

0 commit comments

Comments
 (0)