Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Backport PR #26665 on branch v3.8.x (Clarify loading of backend FigureCanvas and show().) #26675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,9 @@ def switch_backend(newbackend: str) -> None:
old_backend = dict.__getitem__(rcParams, 'backend')

module = importlib.import_module(cbook._backend_module_name(newbackend))
canvas_class = module.FigureCanvas

required_framework = module.FigureCanvas.required_interactive_framework
required_framework = canvas_class.required_interactive_framework
if required_framework is not None:
current_framework = cbook._get_running_interactive_framework()
if (current_framework and required_framework
Expand Down Expand Up @@ -369,8 +370,6 @@ class backend_mod(matplotlib.backend_bases._Backend):
# update backend_mod accordingly; also, per-backend customization of
# draw_if_interactive is disabled.
if new_figure_manager is None:
# Only try to get the canvas class if have opted into the new scheme.
canvas_class = backend_mod.FigureCanvas

def new_figure_manager_given_figure(num, figure):
return canvas_class.new_manager(figure, num)
Expand All @@ -394,8 +393,7 @@ def draw_if_interactive() -> None:

# If the manager explicitly overrides pyplot_show, use it even if a global
# show is already present, as the latter may be here for backcompat.
manager_class = getattr(getattr(backend_mod, "FigureCanvas", None),
"manager_class", None)
manager_class = getattr(canvas_class, "manager_class", None)
# We can't compare directly manager_class.pyplot_show and FMB.pyplot_show because
# pyplot_show is a classmethod so the above constructs are bound classmethods, and
# thus always different (being bound to different classes). We also have to use
Expand All @@ -405,6 +403,10 @@ def draw_if_interactive() -> None:
if (show is None
or (manager_pyplot_show is not None
and manager_pyplot_show != base_pyplot_show)):
if not manager_pyplot_show:
raise ValueError(
f"Backend {newbackend} defines neither FigureCanvas.manager_class nor "
f"a toplevel show function")
_pyplot_show = cast('Any', manager_class).pyplot_show
backend_mod.show = _pyplot_show # type: ignore[method-assign]

Expand Down