Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use() so that it is possible to reset the rcParam. #1028

Merged
merged 1 commit into from Jul 21, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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