From 5bb3c2ac695567eb218534169acc6b4dc6b81292 Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Thu, 10 May 2012 15:56:37 -0400 Subject: [PATCH 01/11] Add rcfile (loads parameters from a file). --- lib/matplotlib/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index cbb7c948ef7b..fabf476a31a4 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -710,6 +710,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: @@ -892,6 +898,13 @@ def rcdefaults(): """ rcParams.update(rcParamsDefault) +def rcfile(fname): + """ + Update rc params from file. + """ + rcParams.update(rc_params_from_file(fname)) + + def rc_file_defaults(): """ Restore the default rc params from the original matplotlib rc that From 395437768adf67967ad8e39f99813d03b97d6faf Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Fri, 11 May 2012 16:26:20 -0400 Subject: [PATCH 02/11] Add rc_context and rename rcfile to rc_file. rc_context is a context manager that optionally accepts a file name: with mpl.rc_context(): mpl.rc('lines', linewidth=22) ... or with mpl.rc_context(fname): ... --- lib/matplotlib/__init__.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index fabf476a31a4..62b3539580af 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -173,7 +173,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 + '' @@ -898,13 +897,25 @@ def rcdefaults(): """ rcParams.update(rcParamsDefault) -def rcfile(fname): + +def rc_file(fname): """ Update rc params from file. """ rcParams.update(rc_params_from_file(fname)) +class rc_context(object): + def __init__(self, fname=None): + self.fname = fname + def __enter__(self): + self._rcparams = rcParams.copy() + if self.fname: + rc_file(self.fname) + 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 From 44f2d83f3375e695bb89435cadcdd0b2c9f9582c Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Fri, 25 May 2012 21:22:50 -0400 Subject: [PATCH 03/11] Add docstring to rc_context and accept a dictionary too. --- lib/matplotlib/__init__.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 62b3539580af..53ebf556706d 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -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) From a54325e50ab6e4343af2d4123f4da7ef00193c87 Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Sat, 11 Aug 2012 22:41:14 -0400 Subject: [PATCH 04/11] Add test_rcparams.py to test rc_file and rc_context. --- lib/matplotlib/tests/mpl.rc | 1 + lib/matplotlib/tests/test_rcparams.py | 35 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 lib/matplotlib/tests/mpl.rc create mode 100644 lib/matplotlib/tests/test_rcparams.py diff --git a/lib/matplotlib/tests/mpl.rc b/lib/matplotlib/tests/mpl.rc new file mode 100644 index 000000000000..26a2b4326bf0 --- /dev/null +++ b/lib/matplotlib/tests/mpl.rc @@ -0,0 +1 @@ +lines.linewidth: 33 diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py new file mode 100644 index 000000000000..27107c90114e --- /dev/null +++ b/lib/matplotlib/tests/test_rcparams.py @@ -0,0 +1,35 @@ +import os + +import matplotlib as mpl +mpl.rc('text', usetex=False) +mpl.rc('lines', linewidth=22) + +fname = os.path.abspath(os.path.dirname(__file__)) + os.sep + 'mpl.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 + mpl.rc_file(fname) + assert mpl.rcParams['lines.linewidth'] == 33 + + +if __name__ == '__main__': + test_rcparams() From 2430747e8b5a512a7f648761579ccfb14ca65d2b Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Sun, 12 Aug 2012 10:03:20 -0400 Subject: [PATCH 05/11] Update/tidy test_rcparams.py and move mpl.rc to test_rcparams.rc. --- lib/matplotlib/__init__.py | 1 + lib/matplotlib/tests/mpl.rc | 1 - lib/matplotlib/tests/test_rcparams.py | 11 +++++++---- lib/matplotlib/tests/test_rcparams.rc | 3 +++ 4 files changed, 11 insertions(+), 5 deletions(-) delete mode 100644 lib/matplotlib/tests/mpl.rc create mode 100644 lib/matplotlib/tests/test_rcparams.rc diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 53ebf556706d..93266aacedb6 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1050,6 +1050,7 @@ def tk_window_focus(): 'matplotlib.tests.test_dates', 'matplotlib.tests.test_spines', 'matplotlib.tests.test_image', + 'matplotlib.tests.test_rcparams', 'matplotlib.tests.test_simplification', 'matplotlib.tests.test_mathtext', 'matplotlib.tests.test_text', diff --git a/lib/matplotlib/tests/mpl.rc b/lib/matplotlib/tests/mpl.rc deleted file mode 100644 index 26a2b4326bf0..000000000000 --- a/lib/matplotlib/tests/mpl.rc +++ /dev/null @@ -1 +0,0 @@ -lines.linewidth: 33 diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 27107c90114e..4666c41274c5 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -4,8 +4,8 @@ mpl.rc('text', usetex=False) mpl.rc('lines', linewidth=22) -fname = os.path.abspath(os.path.dirname(__file__)) + os.sep + 'mpl.rc' - +fname = os.path.join(os.path.dirname(__file__), 'test_rcparams.rc') + def test_rcparams(): usetex = mpl.rcParams['text.usetex'] @@ -27,8 +27,11 @@ def test_rcparams(): assert mpl.rcParams['lines.linewidth'] == linewidth # test rc_file - mpl.rc_file(fname) - assert mpl.rcParams['lines.linewidth'] == 33 + try: + mpl.rc_file(fname) + assert mpl.rcParams['lines.linewidth'] == 33 + finally: + mpl.rcParams['lines.linewidth'] = linewidth if __name__ == '__main__': 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 From ceb66d46e4585cf16aeafab5dfec98475be7969e Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Thu, 10 May 2012 15:56:37 -0400 Subject: [PATCH 06/11] Add rcfile (loads parameters from a file). --- lib/matplotlib/__init__.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 3d1d44a38d15..7f34e1d35908 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -716,6 +716,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 +904,13 @@ def rcdefaults(): """ rcParams.update(rcParamsDefault) +def rcfile(fname): + """ + Update rc params from file. + """ + rcParams.update(rc_params_from_file(fname)) + + def rc_file_defaults(): """ Restore the default rc params from the original matplotlib rc that From 2045bdbd2c00cb71fbce3c03c066b06213aa1567 Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Fri, 11 May 2012 16:26:20 -0400 Subject: [PATCH 07/11] Add rc_context and rename rcfile to rc_file. rc_context is a context manager that optionally accepts a file name: with mpl.rc_context(): mpl.rc('lines', linewidth=22) ... or with mpl.rc_context(fname): ... --- lib/matplotlib/__init__.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 7f34e1d35908..2d1655273cff 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 + '' @@ -904,13 +903,25 @@ def rcdefaults(): """ rcParams.update(rcParamsDefault) -def rcfile(fname): + +def rc_file(fname): """ Update rc params from file. """ rcParams.update(rc_params_from_file(fname)) +class rc_context(object): + def __init__(self, fname=None): + self.fname = fname + def __enter__(self): + self._rcparams = rcParams.copy() + if self.fname: + rc_file(self.fname) + 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 From 0f81dffc05a98fe63e987c2b865a718c53ded7e4 Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Fri, 25 May 2012 21:22:50 -0400 Subject: [PATCH 08/11] Add docstring to rc_context and accept a dictionary too. --- lib/matplotlib/__init__.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 2d1655273cff..b8421c05cdcd 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -912,12 +912,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) From 45cba0fb7fc17a40f7a79f397381519583eacaf9 Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Sat, 11 Aug 2012 22:41:14 -0400 Subject: [PATCH 09/11] Add test_rcparams.py to test rc_file and rc_context. --- lib/matplotlib/tests/mpl.rc | 1 + lib/matplotlib/tests/test_rcparams.py | 35 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 lib/matplotlib/tests/mpl.rc create mode 100644 lib/matplotlib/tests/test_rcparams.py diff --git a/lib/matplotlib/tests/mpl.rc b/lib/matplotlib/tests/mpl.rc new file mode 100644 index 000000000000..26a2b4326bf0 --- /dev/null +++ b/lib/matplotlib/tests/mpl.rc @@ -0,0 +1 @@ +lines.linewidth: 33 diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py new file mode 100644 index 000000000000..27107c90114e --- /dev/null +++ b/lib/matplotlib/tests/test_rcparams.py @@ -0,0 +1,35 @@ +import os + +import matplotlib as mpl +mpl.rc('text', usetex=False) +mpl.rc('lines', linewidth=22) + +fname = os.path.abspath(os.path.dirname(__file__)) + os.sep + 'mpl.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 + mpl.rc_file(fname) + assert mpl.rcParams['lines.linewidth'] == 33 + + +if __name__ == '__main__': + test_rcparams() From c8f5512fdc3b6eb76b40be8c242b5afd0e1f2bf2 Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Sun, 12 Aug 2012 10:03:20 -0400 Subject: [PATCH 10/11] Update/tidy test_rcparams.py and move mpl.rc to test_rcparams.rc. --- lib/matplotlib/__init__.py | 1 + lib/matplotlib/tests/mpl.rc | 1 - lib/matplotlib/tests/test_rcparams.py | 11 +++++++---- lib/matplotlib/tests/test_rcparams.rc | 3 +++ 4 files changed, 11 insertions(+), 5 deletions(-) delete mode 100644 lib/matplotlib/tests/mpl.rc create mode 100644 lib/matplotlib/tests/test_rcparams.rc diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index b8421c05cdcd..f0d82b691516 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1068,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/mpl.rc b/lib/matplotlib/tests/mpl.rc deleted file mode 100644 index 26a2b4326bf0..000000000000 --- a/lib/matplotlib/tests/mpl.rc +++ /dev/null @@ -1 +0,0 @@ -lines.linewidth: 33 diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 27107c90114e..4666c41274c5 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -4,8 +4,8 @@ mpl.rc('text', usetex=False) mpl.rc('lines', linewidth=22) -fname = os.path.abspath(os.path.dirname(__file__)) + os.sep + 'mpl.rc' - +fname = os.path.join(os.path.dirname(__file__), 'test_rcparams.rc') + def test_rcparams(): usetex = mpl.rcParams['text.usetex'] @@ -27,8 +27,11 @@ def test_rcparams(): assert mpl.rcParams['lines.linewidth'] == linewidth # test rc_file - mpl.rc_file(fname) - assert mpl.rcParams['lines.linewidth'] == 33 + try: + mpl.rc_file(fname) + assert mpl.rcParams['lines.linewidth'] == 33 + finally: + mpl.rcParams['lines.linewidth'] = linewidth if __name__ == '__main__': 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 From 121f214fe899af390d39981f409481d9e324052f Mon Sep 17 00:00:00 2001 From: Matthew Emmett Date: Sun, 19 Aug 2012 12:31:54 -0400 Subject: [PATCH 11/11] Add note about rc params to whats_new.rst. --- doc/users/whats_new.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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