Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add rcfile function (which loads rc params from a given file). #861

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

Merged
merged 13 commits into from
Aug 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ interval.
.. plot:: examples/pylab_examples/boxplot_demo3.py


New RC parameter functionality
------------------------------

Matthew Emmett added a function and a context manager to help manage
RC parameters: :func:`~matplotlib.rc_file` and
:class:`~matplotlib.rc_context`. To load RC paramters from a file::

>>> mpl.rc_file('mpl.rc')

To temporarily use RC parameters::

>>> with mpl.rc_context(fname='mpl.rc', rc={'text.usetex': True}):
>>> ...


.. _whats-new-1-1:

new in matplotlib-1.1
Expand Down
54 changes: 53 additions & 1 deletion lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ def byte2str(b): return b
__version__numpy__, numpy.__version__))
del version


def is_string_like(obj):
if hasattr(obj, 'shape'): return 0
try: obj + ''
Expand Down Expand Up @@ -716,6 +715,12 @@ def rc_params(fail_on_error=False):
warnings.warn(message)
return ret

return rc_params_from_file(fname, fail_on_error)


def rc_params_from_file(fname, fail_on_error=False):
"""Load and return params from fname."""

cnt = 0
rc_temp = {}
with open(fname) as fd:
Expand Down Expand Up @@ -898,6 +903,52 @@ def rcdefaults():
"""
rcParams.update(rcParamsDefault)


def rc_file(fname):
"""
Update rc params from file.
"""
rcParams.update(rc_params_from_file(fname))


class rc_context(object):
"""
Return a context manager for managing rc settings.

This allows one to do::

>>> with mpl.rc_context(fname='screen.rc'):
>>> plt.plot(x, a)
>>> with mpl.rc_context(fname='print.rc'):
>>> plt.plot(x, b)
>>> plt.plot(x, c)

The 'a' vs 'x' and 'c' vs 'x' plots would have settings from
'screen.rc', while the 'b' vs 'x' plot would have settings from
'print.rc'.

A dictionary can also be passed to the context manager::

>>> with mpl.rc_context(rc={'text.usetex': True}, fname='screen.rc'):
>>> plt.plot(x, a)

The 'rc' dictionary takes precedence over the settings loaded from
'fname'. Passing a dictionary only is also valid.
"""

def __init__(self, rc=None, fname=None):
self.rcdict = rc
self.fname = fname
def __enter__(self):
self._rcparams = rcParams.copy()
if self.fname:
rc_file(self.fname)
if self.rcdict:
rcParams.update(self.rcdict)
def __exit__(self, type, value, tb):
rcParams.update(self._rcparams)


def rc_file_defaults():
"""
Restore the default rc params from the original matplotlib rc that
Expand Down Expand Up @@ -1017,6 +1068,7 @@ def tk_window_focus():
'matplotlib.tests.test_mathtext',
'matplotlib.tests.test_mlab',
'matplotlib.tests.test_patches',
'matplotlib.tests.test_rcparams',
'matplotlib.tests.test_simplification',
'matplotlib.tests.test_spines',
'matplotlib.tests.test_text',
Expand Down
38 changes: 38 additions & 0 deletions lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import os

import matplotlib as mpl
mpl.rc('text', usetex=False)
mpl.rc('lines', linewidth=22)

fname = os.path.join(os.path.dirname(__file__), 'test_rcparams.rc')

def test_rcparams():

usetex = mpl.rcParams['text.usetex']
linewidth = mpl.rcParams['lines.linewidth']

# test context given dictionary
with mpl.rc_context(rc={'text.usetex': not usetex}):
assert mpl.rcParams['text.usetex'] == (not usetex)
assert mpl.rcParams['text.usetex'] == usetex

# test context given filename (mpl.rc sets linewdith to 33)
with mpl.rc_context(fname=fname):
assert mpl.rcParams['lines.linewidth'] == 33
assert mpl.rcParams['lines.linewidth'] == linewidth

# test context given filename and dictionary
with mpl.rc_context(fname=fname, rc={'lines.linewidth': 44}):
assert mpl.rcParams['lines.linewidth'] == 44
assert mpl.rcParams['lines.linewidth'] == linewidth

# test rc_file
try:
mpl.rc_file(fname)
assert mpl.rcParams['lines.linewidth'] == 33
finally:
mpl.rcParams['lines.linewidth'] = linewidth


if __name__ == '__main__':
test_rcparams()
3 changes: 3 additions & 0 deletions lib/matplotlib/tests/test_rcparams.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# this file is used by the tests in test_rcparams.py

lines.linewidth: 33