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

Skip to content

Commit 68c78c9

Browse files
authored
Merge pull request #14471 from tacaswell/fix_dont_close_on_noop_switch
FIX: don't close figures if switch_backend is a no-op
2 parents f2e2f77 + 32dd6cc commit 68c78c9

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

lib/matplotlib/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,10 @@ def use(backend, *, force=True):
11241124
"""
11251125
Select the backend used for rendering and GUI integration.
11261126
1127+
If pyplot is already imported, `~matplotlib.pyplot.switch_backend` is used
1128+
and if the new backend is different than the current backend, all Figures
1129+
will be closed.
1130+
11271131
Parameters
11281132
----------
11291133
backend : str
@@ -1154,6 +1158,8 @@ def use(backend, *, force=True):
11541158
--------
11551159
:ref:`backends`
11561160
matplotlib.get_backend
1161+
matplotlib.pyplot.switch_backend
1162+
11571163
"""
11581164
name = validate_backend(backend)
11591165
# don't (prematurely) resolve the "auto" backend setting

lib/matplotlib/pyplot.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,24 @@ def _get_backend_mod():
210210

211211
def switch_backend(newbackend):
212212
"""
213-
Close all open figures and set the Matplotlib backend.
213+
Set the pyplot backend.
214214
215-
The argument is case-insensitive. Switching to an interactive backend is
216-
possible only if no event loop for another interactive backend has started.
217-
Switching to and from non-interactive backends is always possible.
215+
Switching to an interactive backend is possible only if no event loop for
216+
another interactive backend has started. Switching to and from
217+
non-interactive backends is always possible.
218+
219+
If the new backend is different than the current backend then all open
220+
Figures will be closed via ``plt.close('all')``.
218221
219222
Parameters
220223
----------
221224
newbackend : str
222-
The name of the backend to use.
225+
The case-insensitive name of the backend to use.
226+
223227
"""
224228
global _backend_mod
225229
# make sure the init is pulled up so we can assign to it later
226230
import matplotlib.backends
227-
close("all")
228231

229232
if newbackend is rcsetup._auto_backend_sentinel:
230233
current_framework = cbook._get_running_interactive_framework()
@@ -261,6 +264,8 @@ def switch_backend(newbackend):
261264
switch_backend("agg")
262265
rcParamsOrig["backend"] = "agg"
263266
return
267+
# have to escape the switch on access logic
268+
old_backend = dict.__getitem__(rcParams, 'backend')
264269

265270
backend_mod = importlib.import_module(
266271
cbook._backend_module_name(newbackend))
@@ -325,6 +330,8 @@ def draw_if_interactive():
325330
# Need to keep a global reference to the backend for compatibility reasons.
326331
# See https://github.com/matplotlib/matplotlib/issues/6092
327332
matplotlib.backends.backend = newbackend
333+
if not cbook._str_equal(old_backend, newbackend):
334+
close("all")
328335

329336
# make sure the repl display hook is installed in case we become
330337
# interactive

lib/matplotlib/tests/test_pyplot.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,14 @@ def test_minor_ticks():
429429
tick_labels = ax.get_yticklabels(minor=True)
430430
assert np.all(tick_pos == np.array([3.5, 6.5]))
431431
assert [l.get_text() for l in tick_labels] == ['a', 'b']
432+
433+
434+
def test_switch_backend_no_close():
435+
plt.switch_backend('agg')
436+
fig = plt.figure()
437+
fig = plt.figure()
438+
assert len(plt.get_fignums()) == 2
439+
plt.switch_backend('agg')
440+
assert len(plt.get_fignums()) == 2
441+
plt.switch_backend('svg')
442+
assert len(plt.get_fignums()) == 0

0 commit comments

Comments
 (0)