Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Do not close figures on backend switch. #26472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 doc/api/next_api_changes/deprecations/26472-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Auto-closing of figures when switching backend
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... is deprecated. Explicitly call ``plt.close("all")`` if necessary. In the
future, allowable backend switches (i.e. those that do not swap a GUI event
loop with another one) will not close existing figures.
6 changes: 3 additions & 3 deletions 6 lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions 9 lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
4 changes: 3 additions & 1 deletion 4 lib/matplotlib/testing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions 4 lib/matplotlib/tests/test_backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
1 change: 1 addition & 0 deletions 1 lib/matplotlib/tests/test_backends_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion 3 lib/matplotlib/tests/test_pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.