8000 Fix up switch_backend() and use(). · matplotlib/matplotlib@865f1c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 865f1c0

Browse files
committed
Fix up switch_backend() and use().
Add an option to use() that allows forcing the configuration change which also reloads the module as necessary. Fix and slightly refactor switch_backend() to make use of this functionality.
1 parent b836275 commit 865f1c0

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

lib/matplotlib/__init__.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -905,11 +905,15 @@ def rc_file_defaults():
905905
or matplotlib.backends is imported for the first time.
906906
"""
907907

908-
def use(arg, warn=True):
908+
def use(arg, warn=True, force=False):
909909
"""
910910
Set the matplotlib backend to one of the known backends.
911911
912-
The argument is case-insensitive.
912+
The argument is case-insensitive. *warn* specifies whether a
913+
warning should be issued if a backend has already been set up.
914+
*force* is an **experimental** flag that tells matplotlib to
915+
attempt to initialize a new backend by reloading the backend
916+
module.
913917
914918
.. note::
915919
@@ -918,25 +922,41 @@ def use(arg, warn=True):
918922
before importing matplotlib.backends. If warn is True, a warning
919923
is issued if you try and call this after pylab or pyplot have been
920924
loaded. In certain black magic use cases, e.g.
921-
:func:`pyplot.switch_backends`, we are doing the reloading necessary to
925+
:func:`pyplot.switch_backend`, we are doing the reloading necessary to
922926
make the backend switch work (in some cases, e.g. pure image
923-
backends) so one can set warn=False to supporess the warnings.
927+
backends) so one can set warn=False to suppress the warnings.
924928
925929
To find out which backend is currently set, see
926930
:func:`matplotlib.get_backend`.
927931
928932
"""
933+
# Check if we've already set up a backend
929934
if 'matplotlib.backends' in sys.modules:
930-
if warn: warnings.warn(_use_error_msg)
931-
return
935+
if warn:
936+
warnings.warn(_use_error_msg)
937+
938+
# Unless we've been told to force it, just return
939+
if not force:
940+
return
941+
need_reload = True
942+
else:
943+
need_reload = False
944+
945+
# Set-up the proper backend name
932946
if arg.startswith('module://'):
933947
name = arg
934948
else:
935949
# Lowercase only non-module backend names (modules are case-sensitive)
936950
arg = arg.lower()
937951
name = validate_backend(arg)
952+
938953
rcParams['backend'] = name
939954

955+
# If needed we reload here because a lot of setup code is triggered on
956+
# module import. See backends/__init__.py for more detail.
957+
if need_reload:
958+
reload(sys.modules['matplotlib.backends'])
959+
940960
def get_backend():
941961
"Returns the current backend."
942962
return rcParams['backend']

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ def switch_backend(newbackend):
117117
"""
118118
close('all')
119119
global new_figure_manager, draw_if_interactive, _show
120-
matplotlib.use(newbackend, warn=False)
121-
reload(matplotlib.backends)
120+
matplotlib.use(newbackend, warn=False, force=True)
122121
from matplotlib.backends import pylab_setup
123122
new_figure_manager, draw_if_interactive, _show = pylab_setup()
124123

0 commit comments

Comments
 (0)
0