@@ -211,6 +211,20 @@ 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
+ """
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
+
214
228
215
229
def switch_backend (newbackend ):
216
230
"""
@@ -298,14 +312,10 @@ class backend_mod(matplotlib.backend_bases._Backend):
298
312
# Need to keep a global reference to the backend for compatibility reasons.
299
313
# See https://github.com/matplotlib/matplotlib/issues/6092
300
314
matplotlib .backends .backend = newbackend
301
- switch_backend ._ever_called = True
302
- switch_backend ._ever_called = False
303
315
304
316
305
317
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 )
318
+ if (_get_required_interactive_framework (_get_backend_mod ())
309
319
and threading .current_thread () is not threading .main_thread ()):
310
320
_api .warn_external (
311
321
"Starting a Matplotlib GUI outside of the main thread will likely "
@@ -316,7 +326,7 @@ def _warn_if_gui_out_of_main_thread():
316
326
def new_figure_manager (* args , ** kwargs ):
317
327
"""Create a new figure manager instance."""
318
328
_warn_if_gui_out_of_main_thread ()
319
- return _backend_mod .new_figure_manager (* args , ** kwargs )
329
+ return _get_backend_mod () .new_figure_manager (* args , ** kwargs )
320
330
321
331
322
332
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -329,9 +339,7 @@ def draw_if_interactive(*args, **kwargs):
329
339
End users will typically not have to call this function because the
330
340
the interactive mode takes care of this.
331
341
"""
332
- if not switch_backend ._ever_called :
333
- _initial_switch_backend ()
334
- return _backend_mod .draw_if_interactive (* args , ** kwargs )
342
+ return _get_backend_mod ().draw_if_interactive (* args , ** kwargs )
335
343
336
344
337
345
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -380,7 +388,7 @@ def show(*args, **kwargs):
380
388
explicitly there.
381
389
"""
382
390
_warn_if_gui_out_of_main_thread ()
383
- return _backend_mod .show (* args , ** kwargs )
391
+ return _get_backend_mod () .show (* args , ** kwargs )
384
392
385
393
386
394
def isinteractive ():
0 commit comments