8000 pyplot.style.use() to accept pathlib.Path objects as arguments by omsitapara23 · Pull Request #15149 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
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
2 changes: 1 addition & 1 deletion lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):

Parameters
----------
fname : str
fname : str or path-like
Name of file parsed for Matplotlib settings.
fail_on_error : bool
If True, raise an error when the parser fails to convert a parameter.
Expand Down
23 changes: 14 additions & 9 deletions lib/matplotlib/style/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def use(style):

Parameters
----------
style : str, dict, or list
style : str, dict, Path or list
A style specification. Valid options are:

+------+-------------------------------------------------------------+
Expand All @@ -82,22 +82,25 @@ def use(style):
| dict | Dictionary with valid key/value pairs for |
| | `matplotlib.rcParams`. |
+------+-------------------------------------------------------------+
| list | A list 8000 of style specifiers (str or dict) applied from first |
| | to last in the list. |
| Path | A path-like object which is a path to a style file. |
+------+-------------------------------------------------------------+
| list | A list of style specifiers (str, Path or dict) applied from |
| | first to last in the list. |
+------+-------------------------------------------------------------+

"""
style_alias = {'mpl20': 'default',
'mpl15': 'classic'}
if isinstance(style, str) or hasattr(style, 'keys'):
# If name is a single str or dict, make it a single element list.
if isinstance(style, (str, Path)) or hasattr(style, 'keys'):
# If name is a single str, Path or dict, make it a single element list.
styles = [style]
else:
styles = style

styles = (style_alias.get(s, s) if isinstance(s, str) else s
for s in styles)
for style in styles:
if not isinstance(style, str):
if not isinstance(style, (str, Path)):
_apply_style(style)
elif style == 'default':
# Deprecation warnings were already handled when creating
Expand All @@ -123,7 +126,7 @@ def context(style, after_reset=False):

Parameters
----------
style : str, dict, or list
style : str, dict, Path or list
A style specification. Valid options are:

+------+-------------------------------------------------------------+
Expand All @@ -133,8 +136,10 @@ def context(style, after_reset=False):
| dict | Dictionary with valid key/value pairs for |
| | `matplotlib.rcParams`. |
+------+-------------------------------------------------------------+
| list | A list of style specifiers (str or dict) applied from first |
| | to last in the list. |
| Path | A path-like object which is a path to a style file. |
+------+-------------------------------------------------------------+
| list | A list of style specifiers (str, Path or dict) applied from |
| | first to last in the list. |
+------+-------------------------------------------------------------+

after_reset : bool
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ def test_use_url(tmpdir):
assert mpl.rcParams['axes.facecolor'] == "#adeade"


def test_single_path(tmpdir):
mpl.rcParams[PARAM] = 'gray'
temp_file = f'text.{STYLE_EXTENSION}'
path = Path(tmpdir, temp_file)
path.write_text(f'{PARAM} : {VALUE}')
with style.context(path):
assert mpl.rcParams[PARAM] == VALUE
assert mpl.rcParams[PARAM] == 'gray'


def test_context():
mpl.rcParams[PARAM] = 'gray'
with temp_style('test', DUMMY_SETTINGS):
Expand Down
0