@@ -104,7 +104,6 @@ def _copy_docstring_and_deprecators(method, func=None):
104
104
105
105
## Global ##
106
106
107
-
108
107
_IP_REGISTERED = None
109
108
_INSTALL_FIG_OBSERVER = False
110
109
@@ -202,6 +201,28 @@ def _get_required_interactive_framework(backend_mod):
202
201
return getattr (
203
202
backend_mod .FigureCanvas , "required_interactive_framework" , None )
204
203
204
+ _backend_mod = None
205
+
206
+
207
+ def _get_backend_mod ():
208
+ """
209
+ Ensure that a backend is selected and return it.
210
+
211
+ This is currently private, but may be made public in the future.
212
+ """
213
+ if _backend_mod is None :
214
+ # Use __getitem__ here to avoid going through the fallback logic (which
215
+ # will (re)import pyplot and then call switch_backend if we need to
216
+ # resolve the auto sentinel)
217
+ switch_backend (dict .__getitem__ (rcParams , "backend" ))
218
+ # Just to be safe. Interactive mode can be turned on without calling
219
+ # `plt.ion()` so register it again here. This is safe because multiple
220
+ # calls to `install_repl_displayhook` are no-ops and the registered
221
+ # function respects `mpl.is_interactive()` to determine if it should
222
+ # trigger a draw.
223
+ install_repl_displayhook ()
224
+ return _backend_mod
225
+
205
226
206
227
def switch_backend (newbackend ):
207
228
"""
@@ -292,7 +313,7 @@ class backend_mod(matplotlib.backend_bases._Backend):
292
313
293
314
294
315
def _warn_if_gui_out_of_main_thread ():
295
- if (_get_required_interactive_framework (_backend_mod )
316
+ if (_get_required_interactive_framework (_get_backend_mod () )
296
317
and threading .current_thread () is not threading .main_thread ()):
297
318
_api .warn_external (
298
319
"Starting a Matplotlib GUI outside of the main thread will likely "
@@ -303,7 +324,7 @@ def _warn_if_gui_out_of_main_thread():
303
324
def new_figure_manager (* args , ** kwargs ):
304
325
"""Create a new figure manager instance."""
305
326
_warn_if_gui_out_of_main_thread ()
306
- return _backend_mod .new_figure_manager (* args , ** kwargs )
327
+ return _get_backend_mod () .new_figure_manager (* args , ** kwargs )
307
328
308
329
309
330
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -316,7 +337,7 @@ def draw_if_interactive(*args, **kwargs):
316
337
End users will typically not have to call this function because the
317
338
the interactive mode takes care of this.
318
339
"""
319
- return _backend_mod .draw_if_interactive (* args , ** kwargs )
340
+ return _get_backend_mod () .draw_if_interactive (* args , ** kwargs )
320
341
321
342
322
343
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -365,7 +386,7 @@ def show(*args, **kwargs):
365
386
explicitly there.
366
387
"""
367
388
_warn_if_gui_out_of_main_thread ()
368
- return _backend_mod .show (* args , ** kwargs )
389
+ return _get_backend_mod () .show (* args , ** kwargs )
369
390
370
391
371
392
def isinteractive ():
@@ -2226,15 +2247,6 @@ def polar(*args, **kwargs):
2226
2247
set (_interactive_bk ) - {'WebAgg' , 'nbAgg' })
2227
2248
and cbook ._get_running_interactive_framework ()):
2228
2249
dict .__setitem__ (rcParams , "backend" , rcsetup ._auto_backend_sentinel )
2229
- # Set up the backend.
2230
- switch_backend (rcParams ["backend" ])
2231
-
2232
- # Just to be safe. Interactive mode can be turned on without
2233
- # calling `plt.ion()` so register it again here.
2234
- # This is safe because multiple calls to `install_repl_displayhook`
2235
- # are no-ops and the registered function respect `mpl.is_interactive()`
2236
- # to determine if they should trigger a draw.
2237
- install_repl_displayhook ()
2238
2250
2239
2251
2240
2252
################# REMAINING CONTENT GENERATED BY boilerplate.py ##############
0 commit comments