Skip to content

Update docs of rc_context() #17162

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 2 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
42 changes: 24 additions & 18 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
Parameters
----------
fname : str or path-like
Name of file parsed for Matplotlib settings.
A file with Matplotlib rc settings.
fail_on_error : bool
If True, raise an error when the parser fails to convert a parameter.
use_default_template : bool
Expand Down Expand Up @@ -1019,8 +1019,8 @@ def rc_file(fname, *, use_default_template=True):

Parameters
----------
fname : str
Name of file parsed for matplotlib settings.
fname : str or path-like
A file with Matplotlib rc settings.

use_default_template : bool
If True, initialize with default parameters before updating with those
Expand All @@ -1040,29 +1040,35 @@ def rc_file(fname, *, use_default_template=True):
@contextlib.contextmanager
def rc_context(rc=None, fname=None):
"""
Return a context manager for managing rc settings.

This allows one to do::
Return a context manager for temporarily changing rcParams.

with mpl.rc_context(fname='screen.rc'):
plt.plot(x, a) # uses 'screen.rc'
with mpl.rc_context(fname='print.rc'):
plt.plot(x, b) # uses 'print.rc'
plt.plot(x, c) # uses 'screen.rc'

A dictionary can also be passed to the context manager::
Parameters
----------
rc : dict
The rcParams to temporarily set.
fname : str or path-like
A file with Matplotlib rc settings. If both *fname* and *rc* are given,
settings from *rc* take precedence.

with mpl.rc_context(rc={'text.usetex': True}, fname='screen.rc'):
plt.plot(x, a)
See Also
--------
:ref:`customizing-with-matplotlibrc-files`

The *rc* dictionary takes precedence over the settings loaded from *fname*.
Passing a dictionary only is also valid. For example, a common usage is::
Examples
--------
Passing explicit values via a dict::

with mpl.rc_context({'interactive': False}):
fig, ax = plt.subplots()
ax.plot(range(3), range(3))
fig.savefig('A.png', format='png')
fig.savefig('example.png')
plt.close(fig)

Loading settings from a file::

with mpl.rc_context(fname='print.rc'):
plt.plot(x, y) # uses 'print.rc'

"""
orig = rcParams.copy()
try:
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def print_png(self, filename_or_obj, *args,

Parameters
----------
filename_or_obj : str or PathLike or file-like object
filename_or_obj : str or path-like or file-like
The file to write to.

metadata : dict, optional
Expand Down Expand Up @@ -526,7 +526,7 @@ def print_jpg(self, filename_or_obj, *args, dryrun=False, pil_kwargs=None,

Parameters
----------
filename_or_obj : str or PathLike or file-like object
filename_or_obj : str or path-like or file-like
The file to write to.

Other Parameters
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def to_filehandle(fname, flag='r', return_opened=False, encoding=None):

Parameters
----------
fname : str or path-like or file-like object
fname : str or path-like or file-like
If `str` or `os.PathLike`, the file is opened using the flags specified
by *flag* and *encoding*. If a file-like object, it is passed through.
flag : str, default 'r'
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ def savefig(self, fname, *, transparent=None, **kwargs):

Parameters
----------
fname : str or PathLike or file-like object
fname : str or path-like or file-like
A path, or a Python file-like object, or
possibly some backend-dependent object such as
`matplotlib.backends.backend_pdf.PdfPages`.
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,

Parameters
----------
fname : str or PathLike or file-like
fname : str or path-like or file-like
A path or a file-like object to store the image in.
If *format* is not set, then the output format is inferred from the
extension of *fname*, if any, and from :rc:`savefig.format` otherwise.
Expand Down