diff --git a/doc/users/whats_new.rst b/doc/users/whats_new.rst index cc6107ef5421..b7a2f890662d 100644 --- a/doc/users/whats_new.rst +++ b/doc/users/whats_new.rst @@ -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 diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 3d1d44a38d15..f0d82b691516 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -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 + '' @@ -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: @@ -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 @@ -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', diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py new file mode 100644 index 000000000000..4666c41274c5 --- /dev/null +++ b/lib/matplotlib/tests/test_rcparams.py @@ -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() diff --git a/lib/matplotlib/tests/test_rcparams.rc b/lib/matplotlib/tests/test_rcparams.rc new file mode 100644 index 000000000000..af7345aa1be1 --- /dev/null +++ b/lib/matplotlib/tests/test_rcparams.rc @@ -0,0 +1,3 @@ +# this file is used by the tests in test_rcparams.py + +lines.linewidth: 33