Skip to content

Commit

Permalink
Merge pull request #1028 from dopplershift/backend_switch
Browse files Browse the repository at this point in the history
Fix use() so that it is possible to reset the rcParam.
  • Loading branch information
WeatherGod committed Jul 21, 2012
2 parents 58581c7 + 865f1c0 commit 60d869c
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
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
Expand Up @@ -122,8 +122,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 60d869c

Please sign in to comment.