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

Skip to content

Commit 0f81dff

Browse files
Matthew Emmettmemmett
Matthew Emmett
authored andcommitted
Add docstring to rc_context and accept a dictionary too.
1 parent 2045bdb commit 0f81dff

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

lib/matplotlib/__init__.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,12 +912,39 @@ def rc_file(fname):
912912

913913

914914
class rc_context(object):
915-
def __init__(self, fname=None):
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
916941
self.fname = fname
917942
def __enter__(self):
918943
self._rcparams = rcParams.copy()
919944
if self.fname:
920945
rc_file(self.fname)
946+
if self.rcdict:
947+
rcParams.update(self.rcdict)
921948
def __exit__(self, type, value, tb):
922949
rcParams.update(self._rcparams)
923950

0 commit comments

Comments
 (0)