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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add docstring to rc_context and accept a dictionary too.
  • Loading branch information
Matthew Emmett committed May 26, 2012
commit 44f2d83f3375e695bb89435cadcdd0b2c9f9582c
29 changes: 28 additions & 1 deletion lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,12 +906,39 @@ def rc_file(fname):


class rc_context(object):
def __init__(self, fname=None):
"""
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)

Expand Down