File tree 2 files changed +16
-1
lines changed
2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -223,7 +223,6 @@ def switch_backend(newbackend):
223
223
global _backend_mod
224
224
# make sure the init is pulled up so we can assign to it later
225
225
import matplotlib .backends
226
- close ("all" )
227
226
228
227
if newbackend is rcsetup ._auto_backend_sentinel :
229
228
current_framework = cbook ._get_running_interactive_framework ()
@@ -260,6 +259,8 @@ def switch_backend(newbackend):
260
259
switch_backend ("agg" )
261
260
rcParamsOrig ["backend" ] = "agg"
262
261
return
262
+ # have to escape the switch on access logic
263
+ old_backend = dict .__getitem__ (rcParams , 'backend' )
263
264
264
265
backend_mod = importlib .import_module (
265
266
cbook ._backend_module_name (newbackend ))
@@ -323,6 +324,9 @@ def draw_if_interactive():
323
324
# Need to keep a global reference to the backend for compatibility reasons.
324
325
# See https://github.com/matplotlib/matplotlib/issues/6092
325
326
matplotlib .backends .backend = newbackend
327
+ if not (isinstance (old_backend , str ) and
328
+ old_backend .lower () == newbackend .lower ()):
329
+ close ("all" )
326
330
327
331
# make sure the repl display hook is installed in case we become
328
332
# interactive
Original file line number Diff line number Diff line change @@ -398,3 +398,14 @@ def test_minor_ticks():
398
398
tick_labels = ax .get_yticklabels (minor = True )
399
399
assert np .all (tick_pos == np .array ([3.5 , 6.5 ]))
400
400
assert [l .get_text () for l in tick_labels ] == ['a' , 'b' ]
401
+
402
+
403
+ def test_switch_backend_no_close ():
404
+ plt .switch_backend ('agg' )
405
+ fig = plt .figure ()
406
+ fig = plt .figure ()
407
+ assert len (plt .get_fignums ()) == 2
408
+ plt .switch_backend ('agg' )
409
+ assert len (plt .get_fignums ()) == 2
410
+ plt .switch_backend ('svg' )
411
+ assert len (plt .get_fignums ()) == 0
You can’t perform that action at this time.
0 commit comments