diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 378dfef082b6..3a08cec7ad1c 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -2244,7 +2244,8 @@ def getname_val(identifier): # requested, ignore rcParams['backend'] and force selection of a backend that # is compatible with the current running interactive framework. if (rcParams["backend_fallback"] - and dict.__getitem__(rcParams, "backend") in _interactive_bk + and dict.__getitem__(rcParams, "backend") in ( + set(_interactive_bk) - {'WebAgg', 'nbAgg'}) and cbook._get_running_interactive_framework()): dict.__setitem__(rcParams, "backend", rcsetup._auto_backend_sentinel) # Set up the backend. diff --git a/lib/matplotlib/tests/test_backend_webagg.py b/lib/matplotlib/tests/test_backend_webagg.py new file mode 100644 index 000000000000..220670aa4da2 --- /dev/null +++ b/lib/matplotlib/tests/test_backend_webagg.py @@ -0,0 +1,28 @@ +import subprocess +import os +import sys +import pytest + + +@pytest.mark.parametrize("backend", ["webagg", "nbagg"]) +def test_webagg_fallback(backend): + if backend == "nbagg": + pytest.importorskip("IPython") + env = {} + if os.name == "nt": + env = dict(os.environ) + else: + env = {"DISPLAY": ""} + + env["MPLBACKEND"] = backend + + test_code = ( + "import os;" + + f"assert os.environ['MPLBACKEND'] == '{backend}';" + + "import matplotlib.pyplot as plt; " + + "print(plt.get_backend());" + f"assert '{backend}' == plt.get_backend().lower();" + ) + ret = subprocess.call([sys.executable, "-c", test_code], env=env) + + assert ret == 0