Skip to content

Commit e39d640

Browse files
authored
Fix: Handle inline backends in plt_show (#14076)
1 parent a2b236f commit e39d640

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

doc/changes/dev/14076.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix figures not being displayed in notebook-style frontends when using a Matplotlib inline backend, by `Mingjian He`_.

mne/viz/tests/test_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
centers_to_edges,
3131
compare_fiff,
3232
concatenate_images,
33+
plt_show,
3334
)
3435

3536
base_dir = Path(__file__).parents[2] / "io" / "tests" / "data"
@@ -46,6 +47,24 @@ def test_setup_vmin_vmax_warns():
4647
_setup_vmin_vmax(data=[-1, 0], vmin=None, vmax=None, norm=True)
4748

4849

50+
@pytest.mark.parametrize(
51+
("backend", "expected"),
52+
(("module://Matplotlib_Inline.backend_inline", "pyplot"), ("QtAgg", "figure")),
53+
)
54+
def test_plt_show_backend(monkeypatch, backend, expected):
55+
"""Test backend-specific show paths."""
56+
calls = []
57+
58+
class Figure:
59+
def show(self, **kwargs):
60+
calls.append(("figure", kwargs))
61+
62+
monkeypatch.setattr("matplotlib.get_backend", lambda: backend)
63+
monkeypatch.setattr(plt, "show", lambda **kwargs: calls.append(("pyplot", kwargs)))
64+
plt_show(fig=Figure(), block=True)
65+
assert calls == [(expected, {"block": True})]
66+
67+
4968
def test_get_color_list():
5069
"""Test getting a colormap from rcParams."""
5170
with rc_context({"axes.prop_cycle": cycler(color=["#ff0000", "#00ff00"])}):

mne/viz/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ def plt_show(show=True, fig=None, **kwargs):
157157
backend = get_backend()
158158
if show and backend != "agg":
159159
logger.debug(f"Showing plot for backend {repr(backend)}")
160-
(fig or plt).show(**kwargs)
160+
# if backend is inline and therefore non-interactive,
161+
# fig.show() fails with UserWarning. See gh-14076
162+
if "inline" in str(backend).lower():
163+
plt.show(**kwargs)
164+
else:
165+
(fig or plt).show(**kwargs)
161166

162167

163168
def _show_browser(show=True, block=True, fig=None, **kwargs):

0 commit comments

Comments
 (0)