MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / switch_backend

Function switch_backend

lib/matplotlib/pyplot.py:394–547  ·  view source on GitHub ↗

Set the pyplot backend. Switching to an interactive backend is possible only if no event loop for another interactive backend has started. Switching to and from non-interactive backends is always possible. Parameters ---------- newbackend : str The case-insens

(newbackend: str)

Source from the content-addressed store, hash-verified

392
393
394def switch_backend(newbackend: str) -> None:
395 """
396 Set the pyplot backend.
397
398 Switching to an interactive backend is possible only if no event loop for
399 another interactive backend has started. Switching to and from
400 non-interactive backends is always possible.
401
402 Parameters
403 ----------
404 newbackend : str
405 The case-insensitive name of the backend to use.
406
407 """
408 global _backend_mod
409 # make sure the init is pulled up so we can assign to it later
410 import matplotlib.backends
411
412 if newbackend is rcsetup._auto_backend_sentinel:
413 current_framework = cbook._get_running_interactive_framework()
414
415 if (current_framework and
416 (backend := backend_registry.backend_for_gui_framework(
417 current_framework))):
418 candidates = [backend]
419 else:
420 candidates = []
421 candidates += [
422 "macosx", "qtagg", "gtk4agg", "gtk3agg", "tkagg", "wxagg"]
423
424 # Don't try to fallback on the cairo-based backends as they each have
425 # an additional dependency (pycairo) over the agg-based backend, and
426 # are of worse quality.
427 for candidate in candidates:
428 try:
429 switch_backend(candidate)
430 except ImportError:
431 _log.debug("Skipping backend candidate %r as loading failed.",
432 candidate, exc_info=True)
433 continue
434 else:
435 rcParamsOrig['backend'] = candidate
436 return
437 else:
438 # Switching to Agg should always succeed; if it doesn't, let the
439 # exception propagate out.
440 switch_backend("agg")
441 rcParamsOrig["backend"] = "agg"
442 return
443 old_backend = rcParams._get('backend') # get without triggering backend resolution
444
445 module = backend_registry.load_backend_module(newbackend)
446 canvas_class = module.FigureCanvas
447
448 required_framework = canvas_class.required_interactive_framework
449 if required_framework is not None:
450 current_framework = cbook._get_running_interactive_framework()
451 if (current_framework and required_framework

Callers 1

_get_backend_modFunction · 0.85

Calls 5

_parse_to_version_infoFunction · 0.90
install_repl_displayhookFunction · 0.85
_getMethod · 0.80
load_backend_moduleMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…