diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 6cb63c82593e..840082cc91ea 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -45,7 +45,7 @@ import matplotlib as mpl from matplotlib import ( _api, backend_tools as tools, cbook, colors, _docstring, text, - _tight_bbox, transforms, widgets, get_backend, is_interactive, rcParams) + _tight_bbox, transforms, widgets, is_interactive, rcParams) from matplotlib._pylab_helpers import Gcf from matplotlib.backend_managers import ToolManager from matplotlib.cbook import _setattr_cm @@ -2736,8 +2736,8 @@ def show(self): # thus warrants a warning. return raise NonGuiException( - f"Matplotlib is currently using {get_backend()}, which is a " - f"non-GUI backend, so cannot show the figure.") + f"{type(self.canvas).__name__} is non-interactive, and thus cannot be " + f"shown") def destroy(self): pass diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index d575a921eace..46c0ccf111ba 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -420,11 +420,16 @@ def draw_if_interactive(): # Need to keep a global reference to the backend for compatibility reasons. # See https://github.com/matplotlib/matplotlib/issues/6092 matplotlib.backends.backend = newbackend # type: ignore[attr-defined] + if not cbook._str_equal(old_backend, newbackend): + if get_fignums(): + _api.warn_deprecated("3.8", message=( + "Auto-close()ing of figures upon backend switching is deprecated since " + "%(since)s and will be removed %(removal)s. To suppress this warning, " + "explicitly call plt.close('all') first.")) close("all") - # make sure the repl display hook is installed in case we become - # interactive + # Make sure the repl display hook is installed in case we become interactive. install_repl_displayhook() diff --git a/lib/matplotlib/testing/conftest.py b/lib/matplotlib/testing/conftest.py index 83de7f9dcf27..c285c247e7b4 100644 --- a/lib/matplotlib/testing/conftest.py +++ b/lib/matplotlib/testing/conftest.py @@ -75,7 +75,9 @@ def mpl_test_settings(request): try: yield finally: - matplotlib.use(prev_backend) + if backend is not None: + plt.close("all") + matplotlib.use(prev_backend) @pytest.fixture diff --git a/lib/matplotlib/tests/test_backend_bases.py b/lib/matplotlib/tests/test_backend_bases.py index c192509699c7..471f46f0effe 100644 --- a/lib/matplotlib/tests/test_backend_bases.py +++ b/lib/matplotlib/tests/test_backend_bases.py @@ -85,13 +85,13 @@ def test_non_gui_warning(monkeypatch): with pytest.warns(UserWarning) as rec: plt.show() assert len(rec) == 1 - assert ('Matplotlib is currently using pdf, which is a non-GUI backend' + assert ('FigureCanvasPdf is non-interactive, and thus cannot be shown' in str(rec[0].message)) with pytest.warns(UserWarning) as rec: plt.gcf().show() assert len(rec) == 1 - assert ('Matplotlib is currently using pdf, which is a non-GUI backend' + assert ('FigureCanvasPdf is non-interactive, and thus cannot be shown' in str(rec[0].message)) diff --git a/lib/matplotlib/tests/test_backends_interactive.py b/lib/matplotlib/tests/test_backends_interactive.py index 4d4ab87b739e..edc7c5caa3d2 100644 --- a/lib/matplotlib/tests/test_backends_interactive.py +++ b/lib/matplotlib/tests/test_backends_interactive.py @@ -167,6 +167,7 @@ def check_alt_backend(alt_backend): fig = plt.figure() assert (type(fig.canvas).__module__ == f"matplotlib.backends.backend_{alt_backend}") + plt.close("all") if importlib.util.find_spec("cairocffi"): check_alt_backend(backend[:-3] + "cairo") diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index 16a927e9e154..68a1de24a561 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -439,7 +439,8 @@ def test_switch_backend_no_close(): assert len(plt.get_fignums()) == 2 plt.switch_backend('agg') assert len(plt.get_fignums()) == 2 - plt.switch_backend('svg') + with pytest.warns(mpl.MatplotlibDeprecationWarning): + plt.switch_backend('svg') assert len(plt.get_fignums()) == 0