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 @@ -224,7 +224,6 @@ def switch_backend(newbackend):
224
224
global _backend_mod
225
225
# make sure the init is pulled up so we can assign to it later
226
226
import matplotlib .backends
227
- close ("all" )
228
227
229
228
if newbackend is rcsetup ._auto_backend_sentinel :
230
229
current_framework = cbook ._get_running_interactive_framework ()
@@ -261,6 +260,8 @@ def switch_backend(newbackend):
261
260
switch_backend ("agg" )
262
261
rcParamsOrig ["backend" ] = "agg"
263
262
return
263
+ # have to escape the switch on access logic
264
+ old_backend = dict .__getitem__ (rcParams , 'backend' )
264
265
265
266
backend_mod = importlib .import_module (
266
267
cbook ._backend_module_name (newbackend ))
@@ -325,6 +326,9 @@ def draw_if_interactive():
325
326
# Need to keep a global reference to the backend for compatibility reasons.
326
327
# See https://github.com/matplotlib/matplotlib/issues/6092
327
328
matplotlib .backends .backend = newbackend
329
+ if not (isinstance (old_backend , str ) and
330
+ old_backend .lower () == newbackend .lower ()):
331
+ close ("all" )
328
332
329
333
# make sure the repl display hook is installed in case we become
330
334
# interactive
Original file line number Diff line number Diff line change @@ -429,3 +429,14 @@ def test_minor_ticks():
429
429
tick_labels = ax .get_yticklabels (minor = True )
430
430
assert np .all (tick_pos == np .array ([3.5 , 6.5 ]))
431
431
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
You can’t perform that action at this time.
0 commit comments