@@ -211,6 +211,19 @@ def _get_required_interactive_framework(backend_mod):
211
211
return getattr (
212
212
backend_mod .FigureCanvas , "required_interactive_framework" , None )
213
213
214
+ _backend_mod = None
215
+
216
+
217
+ def _get_backend_mod ():
218
+ """Ensure that a backend is selected and return it.
219
+
220
+ This is currently private, but may be made public in the future.
221
+ """
222
+ if _backend_mod is None :
223
+ # this will set the global!
224
+ _initial_switch_backend ()
225
+ return _backend_mod
226
+
214
227
215
228
def switch_backend (newbackend ):
216
229
"""
@@ -298,14 +311,10 @@ class backend_mod(matplotlib.backend_bases._Backend):
298
311
# Need to keep a global reference to the backend for compatibility reasons.
299
312
# See https://github.com/matplotlib/matplotlib/issues/6092
300
313
matplotlib .backends .backend = newbackend
301
- switch_backend ._ever_called = True
302
- switch_backend ._ever_called = False
303
314
304
315
305
316
def _warn_if_gui_out_of_main_thread ():
306
- if not switch_backend ._ever_called :
307
- _initial_switch_backend ()
308
- if (_get_required_interactive_framework (_backend_mod )
317
+ if (_get_required_interactive_framework (_get_backend_mod ())
309
318
and threading .current_thread () is not threading .main_thread ()):
310
319
_api .warn_external (
311
320
"Starting a Matplotlib GUI outside of the main thread will likely "
@@ -316,7 +325,7 @@ def _warn_if_gui_out_of_main_thread():
316
325
def new_figure_manager (* args , ** kwargs ):
317
326
"""Create a new figure manager instance."""
318
327
_warn_if_gui_out_of_main_thread ()
319
- return _backend_mod .new_figure_manager (* args , ** kwargs )
328
+ return _get_backend_mod () .new_figure_manager (* args , ** kwargs )
320
329
321
330
322
331
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -329,9 +338,7 @@ def draw_if_interactive(*args, **kwargs):
329
338
End users will typically not have to call this function because the
330
339
the interactive mode takes care of this.
331
340
"""
332
- if not switch_backend ._ever_called :
333
- _initial_switch_backend ()
334
- return _backend_mod .draw_if_interactive (* args , ** kwargs )
341
+ return _get_backend_mod ().draw_if_interactive (* args , ** kwargs )
335
342
336
343
337
344
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -380,7 +387,7 @@ def show(*args, **kwargs):
380
387
explicitly there.
381
388
"""
382
389
_warn_if_gui_out_of_main_thread ()
383
- return _backend_mod .show (* args , ** kwargs )
390
+ return _get_backend_mod () .show (* args , ** kwargs )
384
391
385
392
386
393
def isinteractive ():
0 commit comments