Browse files

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.
  • Loading branch information...
1 parent b836275 commit 865f1c05ee116645e6620fba5f2b54baa46923d5 @dopplershift dopplershift committed Jul 20, 2012
Showing with 27 additions and 8 deletions.
  1. +26 −6 lib/matplotlib/
  2. +1 −2 lib/matplotlib/
@@ -905,11 +905,15 @@ def rc_file_defaults():
or matplotlib.backends is imported for the first time.
-def use(arg, warn=True):
+def use(arg, warn=True, force=False):
Set the matplotlib backend to one of the known backends.
- The argument is case-insensitive.
+ The argument is case-insensitive. *warn* specifies whether a
+ warning should be issued if a backend has already been set up.
+ *force* is an **experimental** flag that tells matplotlib to
+ attempt to initialize a new backend by reloading the backend
+ module.
.. note::
@@ -918,25 +922,41 @@ def use(arg, warn=True):
before importing matplotlib.backends. If warn is True, a warning
is issued if you try and call this after pylab or pyplot have been
loaded. In certain black magic use cases, e.g.
- :func:`pyplot.switch_backends`, we are doing the reloading necessary to
+ :func:`pyplot.switch_backend`, we are doing the reloading necessary to
make the backend switch work (in some cases, e.g. pure image
- backends) so one can set warn=False to supporess the warnings.
+ backends) so one can set warn=False to suppress the warnings.
To find out which backend is currently set, see
+ # Check if we've already set up a backend
if 'matplotlib.backends' in sys.modules:
- if warn: warnings.warn(_use_error_msg)
- return
+ if warn:
+ warnings.warn(_use_error_msg)
+ # Unless we've been told to force it, just return
+ if not force:
+ return
+ need_reload = True
+ else:
+ need_reload = False
+ # Set-up the proper backend name
if arg.startswith('module://'):
name = arg
# Lowercase only non-module backend names (modules are case-sensitive)
arg = arg.lower()
name = validate_backend(arg)
rcParams['backend'] = name
+ # If needed we reload here because a lot of setup code is triggered on
+ # module import. See backends/ for more detail.
+ if need_reload:
+ reload(sys.modules['matplotlib.backends'])
def get_backend():
"Returns the current backend."
return rcParams['backend']
@@ -117,8 +117,7 @@ def switch_backend(newbackend):
global new_figure_manager, draw_if_interactive, _show
- matplotlib.use(newbackend, warn=False)
- reload(matplotlib.backends)
+ matplotlib.use(newbackend, warn=False, force=True)
from matplotlib.backends import pylab_setup
new_figure_manager, draw_if_interactive, _show = pylab_setup()

0 comments on commit 865f1c0

Please sign in to comment.