From e1e0fa68a9cefb919b9460fbc93e8660daa3d746 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Wed, 2 Feb 2022 18:08:44 -0500 Subject: [PATCH] ENH: by-pass creating a local class if backend has a Backend class --- lib/matplotlib/backend_bases.py | 3 +++ lib/matplotlib/pyplot.py | 12 ++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 0c7893b81977..605212c75bfb 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3523,6 +3523,9 @@ class Show(ShowBase): def mainloop(self): return cls.mainloop() + if not hasattr(sys.modules[cls.__module__], "Backend"): + setattr(sys.modules[cls.__module__], "Backend", cls) + setattr(sys.modules[cls.__module__], "Show", Show) return cls diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 45e40feeef54..67e27a9c85ff 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -269,8 +269,16 @@ def switch_backend(newbackend): backend_name = cbook._backend_module_name(newbackend) - class backend_mod(matplotlib.backend_bases._Backend): - locals().update(vars(importlib.import_module(backend_name))) + backend_mod = importlib.import_module(backend_name) + if hasattr(backend_mod, "Backend"): + backend_mod = backend_mod.Backend + else: + class backend_mod(matplotlib.backend_bases._Backend): + locals().update(vars(backend_mod)) + + @classmethod + def mainloop(cls): + return backend_mod.Show().mainloop() required_framework = _get_required_interactive_framework(backend_mod) if required_framework is not None: