@@ -104,6 +104,15 @@ def _copy_docstring_and_deprecators(method, func=None):
104
104
105
105
## Global ##
106
106
107
+ def _initial_switch_backend ():
108
+ switch_backend (dict .__getitem__ (rcParams , "backend" ))
109
+ # Just to be safe. Interactive mode can be turned on without
110
+ # calling `plt.ion()` so register it again here.
111
+ # This is safe because multiple calls to `install_repl_displayhook`
112
+ # are no-ops and the registered function respect `mpl.is_interactive()`
113
+ # to determine if they should trigger a draw.
114
+ install_repl_displayhook ()
115
+
107
116
108
117
_IP_REGISTERED = None
109
118
_INSTALL_FIG_OBSERVER = False
@@ -202,6 +211,20 @@ def _get_required_interactive_framework(backend_mod):
202
211
return getattr (
203
212
backend_mod .FigureCanvas , "required_interactive_framework" , None )
204
213
214
+ _backend_mod = None
215
+
216
+
217
+ def _get_backend_mod ():
218
+ """
219
+ Ensure that a backend is selected and return it.
220
+
221
+ This is currently private, but may be made public in the future.
222
+ """
223
+ if _backend_mod is None :
224
+ # this will set the global!
225
+ _initial_switch_backend ()
226
+ return _backend_mod
227
+
205
228
206
229
def switch_backend (newbackend ):
207
230
"""
@@ -292,7 +315,7 @@ class backend_mod(matplotlib.backend_bases._Backend):
292
315
293
316
294
317
def _warn_if_gui_out_of_main_thread ():
295
- if (_get_required_interactive_framework (_backend_mod )
318
+ if (_get_required_interactive_framework (_get_backend_mod () )
296
319
and threading .current_thread () is not threading .main_thread ()):
297
320
_api .warn_external (
298
321
"Starting a Matplotlib GUI outside of the main thread will likely "
@@ -303,7 +326,7 @@ def _warn_if_gui_out_of_main_thread():
303
326
def new_figure_manager (* args , ** kwargs ):
304
327
"""Create a new figure manager instance."""
305
328
_warn_if_gui_out_of_main_thread ()
306
- return _backend_mod .new_figure_manager (* args , ** kwargs )
329
+ return _get_backend_mod () .new_figure_manager (* args , ** kwargs )
307
330
308
331
309
332
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -316,7 +339,7 @@ def draw_if_interactive(*args, **kwargs):
316
339
End users will typically not have to call this function because the
317
340
the interactive mode takes care of this.
318
341
"""
319
- return _backend_mod .draw_if_interactive (* args , ** kwargs )
342
+ return _get_backend_mod () .draw_if_interactive (* args , ** kwargs )
320
343
321
344
322
345
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -365,7 +388,7 @@ def show(*args, **kwargs):
365
388
explicitly there.
366
389
"""
367
390
_warn_if_gui_out_of_main_thread ()
368
- return _backend_mod .show (* args , ** kwargs )
391
+ return _get_backend_mod () .show (* args , ** kwargs )
369
392
370
393
371
394
def isinteractive ():
@@ -2229,15 +2252,6 @@ def polar(*args, **kwargs):
2229
2252
set (_interactive_bk ) - {'WebAgg' , 'nbAgg' })
2230
2253
and cbook ._get_running_interactive_framework ()):
2231
2254
dict .__setitem__ (rcParams , "backend" , rcsetup ._auto_backend_sentinel )
2232
- # Set up the backend.
2233
- switch_backend (rcParams ["backend" ])
2234
-
2235
- # Just to be safe. Interactive mode can be turned on without
2236
- # calling `plt.ion()` so register it again here.
2237
- # This is safe because multiple calls to `install_repl_displayhook`
2238
- # are no-ops and the registered function respect `mpl.is_interactive()`
2239
- # to determine if they should trigger a draw.
2240
- install_repl_displayhook ()
2241
2255
2242
2256
2243
2257
################# REMAINING CONTENT GENERATED BY boilerplate.py ##############
0 commit comments