Skip to content

Commit 60d869c

Browse files
committed
Merge pull request #1028 from dopplershift/backend_switch
Fix use() so that it is possible to reset the rcParam.
2 parents 58581c7 + 865f1c0 commit 60d869c

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

lib/matplotlib/__init__.py

+26-6
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

+1-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ def switch_backend(newbackend):
122122
"""
123123
close('all')
124124
global new_figure_manager, draw_if_interactive, _show
125-
matplotlib.use(newbackend, warn=False)
126-
reload(matplotlib.backends)
125+
matplotlib.use(newbackend, warn=False, force=True)
127126
from matplotlib.backends import pylab_setup
128127
new_figure_manager, draw_if_interactive, _show = pylab_setup()
129128

0 commit comments

Comments
 (0)