Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb05fb5

Browse files
committed
Test whether it is viable to not close figures on backend switch.
DO NOT MERGE. The desired change would need a deprecation period, but this is just to check on CI that the end state works. Do not `close("all")` figures on (allowable) backend switches, e.g. between qt5agg and qt5cairo, or qt5agg and notebook. The NonGuiException message had to change, as the backend returned by get_backend() may no longer match the actual canvas class.
1 parent f017315 commit bb05fb5

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import matplotlib as mpl
4646
from matplotlib import (
4747
_api, backend_tools as tools, cbook, colors, _docstring, text,
48-
_tight_bbox, transforms, widgets, get_backend, is_interactive, rcParams)
48+
_tight_bbox, transforms, widgets, is_interactive, rcParams)
4949
from matplotlib._pylab_helpers import Gcf
5050
from matplotlib.backend_managers import ToolManager
5151
from matplotlib.cbook import _setattr_cm
@@ -2734,8 +2734,8 @@ def show(self):
27342734
# thus warrants a warning.
27352735
return
27362736
raise NonGuiException(
2737-
f"Matplotlib is currently using {get_backend()}, which is a "
2738-
f"non-GUI backend, so cannot show the figure.")
2737+
f"{type(self.canvas).__name__} is non-interactive, and thus cannot be "
2738+
f"shown")
27392739

27402740
def destroy(self):
27412741
pass

lib/matplotlib/pyplot.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,16 @@ def draw_if_interactive():
391391
# Need to keep a global reference to the backend for compatibility reasons.
392392
# See https://github.com/matplotlib/matplotlib/issues/6092
393393
matplotlib.backends.backend = newbackend
394+
394395
if not cbook._str_equal(old_backend, newbackend):
396+
if get_fignums():
397+
_api.warn_deprecated("3.8", message=(
398+
"Auto-close()ing of figures upon backend switching is deprecated since "
399+
"%(since)s and will be removed %(removal)s. To suppress this warning, "
400+
"explicitly call plt.close('all') first."))
395401
close("all")
396402

397-
# make sure the repl display hook is installed in case we become
398-
# interactive
403+
# Make sure the repl display hook is installed in case we become interactive.
399404
install_repl_displayhook()
400405

401406

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ def test_non_gui_warning(monkeypatch):
8585
with pytest.warns(UserWarning) as rec:
8686
plt.show()
8787
assert len(rec) == 1
88-
assert ('Matplotlib is currently using pdf, which is a non-GUI backend'
88+
assert ('FigureCanvasPdf is non-interactive, and thus cannot be shown'
8989
in str(rec[0].message))
9090

9191
with pytest.warns(UserWarning) as rec:
9292
plt.gcf().show()
9393
assert len(rec) == 1
94-
assert ('Matplotlib is currently using pdf, which is a non-GUI backend'
94+
assert ('FigureCanvasPdf is non-interactive, and thus cannot be shown'
9595
in str(rec[0].message))
9696

9797

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def check_alt_backend(alt_backend):
145145
fig = plt.figure()
146146
assert (type(fig.canvas).__module__ ==
147147
f"matplotlib.backends.backend_{alt_backend}")
148+
plt.close("all")
148149

149150
if importlib.util.find_spec("cairocffi"):
150151
check_alt_backend(backend[:-3] + "cairo")

lib/matplotlib/tests/test_pyplot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ def test_switch_backend_no_close():
439439
assert len(plt.get_fignums()) == 2
440440
plt.switch_backend('agg')
441441
assert len(plt.get_fignums()) == 2
442-
plt.switch_backend('svg')
442+
with pytest.warns(mpl.MatplotlibDeprecationWarning):
443+
plt.switch_backend('svg')
443444
assert len(plt.get_fignums()) == 0
444445

445446

0 commit comments

Comments
 (0)