diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index b7cac2a11fa1..81792d2dc68f 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -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. diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index e151b27b8181..577c9f51e7a5 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -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: +------+-------------------------------------------------------------+ @@ -82,14 +82,17 @@ def use(style): | 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. | + +------+-------------------------------------------------------------+ + """ 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 @@ -97,7 +100,7 @@ def use(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 @@ -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: +------+-------------------------------------------------------------+ @@ -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 diff --git a/lib/matplotlib/tests/test_style.py b/lib/matplotlib/tests/test_style.py index 552b87e4aa14..0accfa005f5f 100644 --- a/lib/matplotlib/tests/test_style.py +++ b/lib/matplotlib/tests/test_style.py @@ -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):