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

Skip to content

Commit 709c709

Browse files
committed
Merge pull request #861 from memmett/master
Add rcfile function (which loads rc params from a given file) and associated context manager.
2 parents 1bd384e + 121f214 commit 709c709

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

doc/users/whats_new.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@ interval.
8989
.. plot:: examples/pylab_examples/boxplot_demo3.py
9090

9191

92+
New RC parameter functionality
93+
------------------------------
94+
95+
Matthew Emmett added a function and a context manager to help manage
96+
RC parameters: :func:`~matplotlib.rc_file` and
97+
:class:`~matplotlib.rc_context`. To load RC paramters from a file::
98+
99+
>>> mpl.rc_file('mpl.rc')
100+
101+
To temporarily use RC parameters::
102+
103+
>>> with mpl.rc_context(fname='mpl.rc', rc={'text.usetex': True}):
104+
>>> ...
105+
106+
92107
.. _whats-new-1-1:
93108

94109
new in matplotlib-1.1

lib/matplotlib/__init__.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ def byte2str(b): return b
178178
__version__numpy__, numpy.__version__))
179179
del version
180180

181-
182181
def is_string_like(obj):
183182
if hasattr(obj, 'shape'): return 0
184183
try: obj + ''
@@ -716,6 +715,12 @@ def rc_params(fail_on_error=False):
716715
warnings.warn(message)
717716
return ret
718717

718+
return rc_params_from_file(fname, fail_on_error)
719+
720+
721+
def rc_params_from_file(fname, fail_on_error=False):
722+
"""Load and return params from fname."""
723+
719724
cnt = 0
720725
rc_temp = {}
721726
with open(fname) as fd:
@@ -898,6 +903,52 @@ def rcdefaults():
898903
"""
899904
rcParams.update(rcParamsDefault)
900905

906+
907+
def rc_file(fname):
908+
"""
909+
Update rc params from file.
910+
"""
911+
rcParams.update(rc_params_from_file(fname))
912+
913+
914+
class rc_context(object):
915+
"""
916+
Return a context manager for managing rc settings.
917+
918+
This allows one to do::
919+
920+
>>> with mpl.rc_context(fname='screen.rc'):
921+
>>> plt.plot(x, a)
922+
>>> with mpl.rc_context(fname='print.rc'):
923+
>>> plt.plot(x, b)
924+
>>> plt.plot(x, c)
925+
926+
The 'a' vs 'x' and 'c' vs 'x' plots would have settings from
927+
'screen.rc', while the 'b' vs 'x' plot would have settings from
928+
'print.rc'.
929+
930+
A dictionary can also be passed to the context manager::
931+
932+
>>> with mpl.rc_context(rc={'text.usetex': True}, fname='screen.rc'):
933+
>>> plt.plot(x, a)
934+
935+
The 'rc' dictionary takes precedence over the settings loaded from
936+
'fname'. Passing a dictionary only is also valid.
937+
"""
938+
939+
def __init__(self, rc=None, fname=None):
940+
self.rcdict = rc
941+
self.fname = fname
942+
def __enter__(self):
943+
self._rcparams = rcParams.copy()
944+
if self.fname:
945+
rc_file(self.fname)
946+
if self.rcdict:
947+
rcParams.update(self.rcdict)
948+
def __exit__(self, type, value, tb):
949+
rcParams.update(self._rcparams)
950+
951+
901952
def rc_file_defaults():
902953
"""
903954
Restore the default rc params from the original matplotlib rc that
@@ -1017,6 +1068,7 @@ def tk_window_focus():
10171068
'matplotlib.tests.test_mathtext',
10181069
'matplotlib.tests.test_mlab',
10191070
'matplotlib.tests.test_patches',
1071+
'matplotlib.tests.test_rcparams',
10201072
'matplotlib.tests.test_simplification',
10211073
'matplotlib.tests.test_spines',
10221074
'matplotlib.tests.test_text',

lib/matplotlib/tests/test_rcparams.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
3+
import matplotlib as mpl
4+
mpl.rc('text', usetex=False)
5+
mpl.rc('lines', linewidth=22)
6+
7+
fname = os.path.join(os.path.dirname(__file__), 'test_rcparams.rc')
8+
9+
def test_rcparams():
10+
11+
usetex = mpl.rcParams['text.usetex']
12+
linewidth = mpl.rcParams['lines.linewidth']
13+
14+
# test context given dictionary
15+
with mpl.rc_context(rc={'text.usetex': not usetex}):
16+
assert mpl.rcParams['text.usetex'] == (not usetex)
17+
assert mpl.rcParams['text.usetex'] == usetex
18+
19+
# test context given filename (mpl.rc sets linewdith to 33)
20+
with mpl.rc_context(fname=fname):
21+
assert mpl.rcParams['lines.linewidth'] == 33
22+
assert mpl.rcParams['lines.linewidth'] == linewidth
23+
24+
# test context given filename and dictionary
25+
with mpl.rc_context(fname=fname, rc={'lines.linewidth': 44}):
26+
assert mpl.rcParams['lines.linewidth'] == 44
27+
assert mpl.rcParams['lines.linewidth'] == linewidth
28+
29+
# test rc_file
30+
try:
31+
mpl.rc_file(fname)
32+
assert mpl.rcParams['lines.linewidth'] == 33
33+
finally:
34+
mpl.rcParams['lines.linewidth'] = linewidth
35+
36+
37+
if __name__ == '__main__':
38+
test_rcparams()

lib/matplotlib/tests/test_rcparams.rc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# this file is used by the tests in test_rcparams.py
2+
3+
lines.linewidth: 33

0 commit comments

Comments
 (0)