8000 Merge pull request #15149 from omsitapara23/path-objects-for-styles · matplotlib/matplotlib@98507c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98507c0

Browse files
authored
Merge pull request #15149 from omsitapara23/path-objects-for-styles
pyplot.style.use() to accept pathlib.Path objects as arguments
2 parents e69699f + 0c3a7ea commit 98507c0

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

lib/matplotlib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
948948
949949
Parameters
950950
----------
951-
fname : str
951+
fname : str or path-like
952952
Name of file parsed for Matplotlib settings.
953953
fail_on_error : bool
954954
If True, raise an error when the parser fails to convert a parameter.

lib/matplotlib/style/core.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def use(style):
7272
7373
Parameters
7474
----------
75-
style : str, dict, or list
75+
style : str, dict, Path or list
7676
A style specification. Valid options are:
7777
7878
+------+-------------------------------------------------------------+
@@ -82,22 +82,25 @@ def use(style):
8282
| dict | Dictionary with valid key/value pairs for |
8383
| | `matplotlib.rcParams`. |
8484
+------+-------------------------------------------------------------+
85-
| list | A list of style specifiers (str or dict) applied from first |
86-
| | to last in the list. |
85+
| Path | A path-like object which is a path to a style file. |
8786
+------+-------------------------------------------------------------+
87+
| list | A list of style specifiers (str, Path or dict) applied from |
88+
| | first to last in the list. |
89+
+------+-------------------------------------------------------------+
90+
8891
"""
8992
style_alias = {'mpl20': 'default',
9093
'mpl15': 'classic'}
91-
if isinstance(style, str) or hasattr(style, 'keys'):
92-
# If name is a single str or dict, make it a single element list.
94+
if isinstance(style, (str, Path)) or hasattr(style, 'keys'):
95+
# If name is a single str, Path or dict, make it a single element list.
9396
styles = [style]
9497
else:
9598
styles = style
9699

97100
styles = (style_alias.get(s, s) if isinstance(s, str) else s
98101
for s in styles)
99102
for style in styles:
100-
if not isinstance(style, str):
103+
if not isinstance(style, (str, Path)):
101104
_apply_style(style)
102105
elif style == 'default':
103106
# Deprecation warnings were already handled when creating
@@ -123,7 +126,7 @@ def context(style, after_reset=False):
123126
124127
Parameters
125128
----------
126-
style : str, dict, or list
129+
style : str, dict, Path or list
127130
A style specification. Valid options are:
128131
129132
+------+-------------------------------------------------------------+
@@ -133,8 +136,10 @@ def context(style, after_reset=False):
133136
| dict | Dictionary with valid key/value pairs for |
134137
| | `matplotlib.rcParams`. |
135138
+------+-------------------------------------------------------------+
136-
| list | A list of style specifiers (str or dict) applied from first |
137-
| | to last in the list. |
139+
| Path | A path-like object which is a path to a style file. |
140+
+------+-------------------------------------------------------------+
141+
| list | A list of style specifiers (str, Path or dict) applied from |
142+
| | first to last in the list. |
138143
+------+-------------------------------------------------------------+
139144
140145
after_reset : bool

lib/matplotlib/tests/test_style.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ def test_use_url(tmpdir):
6868
assert mpl.rcParams['axes.facecolor'] == "#adeade"
6969

7070

71+
def test_single_path(tmpdir):
72+
mpl.rcParams[PARAM] = 'gray'
73+
temp_file = f'text.{STYLE_EXTENSION}'
74+
path = Path(tmpdir, temp_file)
75+
path.write_text(f'{PARAM} : {VALUE}')
76+
with style.context(path):
77+
assert mpl.rcParams[PARAM] == VALUE
78+
assert mpl.rcParams[PARAM] == 'gray'
79+
80+
7181
def test_context():
7282
mpl.rcParams[PARAM] = 'gray'
7383
with temp_style('test', DUMMY_SETTINGS):

0 commit comments

Comments
 (0)
0