7373 FigureCanvasBase , FigureManagerBase , MouseButton )
7474from matplotlib .figure import Figure , FigureBase , figaspect
7575from matplotlib .gridspec import GridSpec , SubplotSpec
76- from matplotlib import rcsetup , rcParamsDefault , rcParamsOrig
76+ from matplotlib import rcParamsDefault , rcParamsOrig # noqa: F401
77+ from matplotlib import rcsetup
7778from matplotlib .artist import Artist
7879from matplotlib .axes import Axes
7980from matplotlib .axes import Subplot # noqa: F401
@@ -385,14 +386,14 @@ def _get_backend_mod() -> type[matplotlib.backend_bases._Backend]:
385386 This is currently private, but may be made public in the future.
386387 """
387388 if _backend_mod is None :
388- # Use rcParams._get("backend") to avoid going through the fallback
389+ # Use rcParams._backend to avoid going through the fallback
389390 # logic (which will (re)import pyplot and then call switch_backend if
390391 # we need to resolve the auto sentinel)
391- switch_backend (rcParams ._get ( "backend" ) )
392+ switch_backend (rcParams ._backend )
392393 return cast (type [matplotlib .backend_bases ._Backend ], _backend_mod )
393394
394395
395- def switch_backend (newbackend : str ) -> None :
396+ def switch_backend (newbackend : str | object ) -> None :
396397 """
397398 Set the pyplot backend.
398399
@@ -405,6 +406,9 @@ def switch_backend(newbackend: str) -> None:
405406 newbackend : str
406407 The case-insensitive name of the backend to use.
407408
409+ As an internal implementation detail, the special value
410+ ``rcsetup._auto_backend_sentinel`` can be used to trigger the backend
411+ selection.
408412 """
409413 global _backend_mod
410414 # make sure the init is pulled up so we can assign to it later
@@ -432,16 +436,16 @@ def switch_backend(newbackend: str) -> None:
432436 _log .debug ("Skipping backend candidate %r as loading failed." ,
433437 candidate , exc_info = True )
434438 continue
435- else :
436- rcParamsOrig ['backend' ] = candidate
437- return
439+ return
438440 else :
439441 # Switching to Agg should always succeed; if it doesn't, let the
440442 # exception propagate out.
441443 switch_backend ("agg" )
442- rcParamsOrig ["backend" ] = "agg"
443444 return
444- old_backend = rcParams ._get ('backend' ) # get without triggering backend resolution
445+ # the only object is the sentinel, which is handled above so that newbackend
446+ # is always a string here.
447+ newbackend = cast (str , newbackend )
448+ old_backend = rcParams ._backend
445449
446450 module = backend_registry .load_backend_module (newbackend )
447451 canvas_class = module .FigureCanvas
@@ -530,7 +534,7 @@ def draw_if_interactive() -> None:
530534 except im .PackageNotFoundError :
531535 pass
532536
533- rcParams [ 'backend' ] = rcParamsDefault [ 'backend' ] = newbackend
537+ rcParams . _backend = newbackend
534538 _backend_mod = backend_mod
535539 for func_name in ["new_figure_manager" , "draw_if_interactive" , "show" ]:
536540 globals ()[func_name ].__signature__ = inspect .signature (
0 commit comments