@@ -266,15 +266,9 @@ def switch_backend(newbackend):
266
266
rcParamsOrig ["backend" ] = "agg"
267
267
return
268
268
269
- # Backends are implemented as modules, but "inherit" default method
270
- # implementations from backend_bases._Backend. This is achieved by
271
- # creating a "class" that inherits from backend_bases._Backend and whose
272
- # body is filled with the module's globals.
273
-
274
- backend_name = cbook ._backend_module_name (newbackend )
275
-
276
- class backend_mod (matplotlib .backend_bases ._Backend ):
277
- locals ().update (vars (importlib .import_module (backend_name )))
269
+ backend_mod = importlib .import_module (
270
+ cbook ._backend_module_name (newbackend ))
271
+ canvas_class = backend_mod .FigureCanvas
278
272
279
273
required_framework = _get_required_interactive_framework (backend_mod )
280
274
if required_framework is not None :
@@ -286,6 +280,36 @@ class backend_mod(matplotlib.backend_bases._Backend):
286
280
"framework, as {!r} is currently running" .format (
287
281
newbackend , required_framework , current_framework ))
288
282
283
+ # Load the new_figure_manager(), draw_if_interactive(), and show()
284
+ # functions from the backend.
285
+
286
+ # Classically, backends can directly export these functions. This should
287
+ # keep working for backcompat.
288
+ new_figure_manager = getattr (backend_mod , "new_figure_manager" , None )
289
+ # draw_if_interactive = getattr(backend_mod, "draw_if_interactive", None)
290
+ # show = getattr(backend_mod, "show", None)
291
+ # In that classical approach, backends are implemented as modules, but
292
+ # "inherit" default method implementations from backend_bases._Backend.
293
+ # This is achieved by creating a "class" that inherits from
294
+ # backend_bases._Backend and whose body is filled with the module globals.
295
+ class backend_mod (matplotlib .backend_bases ._Backend ):
296
+ locals ().update (vars (backend_mod ))
297
+
298
+ # However, the newer approach for defining new_figure_manager (and, in
299
+ # the future, draw_if_interactive and show) is to derive them from canvas
300
+ # methods. In that case, also update backend_mod accordingly.
301
+ if new_figure_manager is None :
302
+ def new_figure_manager_given_figure (num , figure ):
303
+ return canvas_class .new_manager (figure , num )
304
+
305
+ def new_figure_manager (num , * args , FigureClass = Figure , ** kwargs ):
306
+ fig = FigureClass (* args , ** kwargs )
307
+ return new_figure_manager_given_figure (num , fig )
308
+
309
+ backend_mod .new_figure_manager_given_figure = \
310
+ new_figure_manager_given_figure
311
+ backend_mod .new_figure_manager = new_figure_manager
312
+
289
313
_log .debug ("Loaded backend %s version %s." ,
290
314
newbackend , backend_mod .backend_version )
291
315
0 commit comments