File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed
Expand file tree Collapse file tree 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):
223223 global _backend_mod
224224 # make sure the init is pulled up so we can assign to it later
225225 import matplotlib .backends
226- close ("all" )
227226
228227 if newbackend is rcsetup ._auto_backend_sentinel :
229228 current_framework = cbook ._get_running_interactive_framework ()
@@ -260,6 +259,8 @@ def switch_backend(newbackend):
260259 switch_backend ("agg" )
261260 rcParamsOrig ["backend" ] = "agg"
262261 return
262+ # have to escape the switch on access logic
263+ old_backend = dict .__getitem__ (rcParams , 'backend' )
263264
264265 backend_mod = importlib .import_module (
265266 cbook ._backend_module_name (newbackend ))
@@ -323,6 +324,9 @@ def draw_if_interactive():
323324 # Need to keep a global reference to the backend for compatibility reasons.
324325 # See https://github.com/matplotlib/matplotlib/issues/6092
325326 matplotlib .backends .backend = newbackend
327+ if not (isinstance (old_backend , str ) and
328+ old_backend .lower () == newbackend .lower ()):
329+ close ("all" )
326330
327331 # make sure the repl display hook is installed in case we become
328332 # interactive
Original file line number Diff line number Diff line change @@ -398,3 +398,14 @@ def test_minor_ticks():
398398 tick_labels = ax .get_yticklabels (minor = True )
399399 assert np .all (tick_pos == np .array ([3.5 , 6.5 ]))
400400 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