@@ -210,21 +210,24 @@ def _get_backend_mod():
210
210
211
211
def switch_backend (newbackend ):
212
212
"""
213
- Close all open figures and set the Matplotlib backend.
213
+ Set the pyplot backend.
214
214
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')``.
218
221
219
222
Parameters
220
223
----------
221
224
newbackend : str
222
- The name of the backend to use.
225
+ The case-insensitive name of the backend to use.
226
+
223
227
"""
224
228
global _backend_mod
225
229
# make sure the init is pulled up so we can assign to it later
226
230
import matplotlib .backends
227
- close ("all" )
228
231
229
232
if newbackend is rcsetup ._auto_backend_sentinel :
230
233
current_framework = cbook ._get_running_interactive_framework ()
@@ -261,6 +264,8 @@ def switch_backend(newbackend):
261
264
switch_backend ("agg" )
262
265
rcParamsOrig ["backend" ] = "agg"
263
266
return
267
+ # have to escape the switch on access logic
268
+ old_backend = dict .__getitem__ (rcParams , 'backend' )
264
269
265
270
backend_mod = importlib .import_module (
266
271
cbook ._backend_module_name (newbackend ))
@@ -325,6 +330,8 @@ def draw_if_interactive():
325
330
# Need to keep a global reference to the backend for compatibility reasons.
326
331
# See https://github.com/matplotlib/matplotlib/issues/6092
327
332
matplotlib .backends .backend = newbackend
333
+ if not cbook ._str_equal (old_backend , newbackend ):
334
+ close ("all" )
328
335
329
336
# make sure the repl display hook is installed in case we become
330
337
# interactive
0 commit comments