Skip to content

Commit

Permalink
Fix up switch_backend() and use().
Browse files Browse the repository at this point in the history
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
dopplershift committed Jul 21, 2012
1 parent b836275 commit 865f1c0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
32 changes: 26 additions & 6 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand All @@ -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
:func:`matplotlib.get_backend`.
"""
# 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
else:
# 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/__init__.py for more detail.
if need_reload:
reload(sys.modules['matplotlib.backends'])

def get_backend():
"Returns the current backend."
return rcParams['backend']
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ def switch_backend(newbackend):
"""
close('all')
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()

Expand Down

0 comments on commit 865f1c0

Please sign in to comment.