From cd4b3118bc4459d1eb7714cb03cf159f9d32f845 Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Wed, 28 Aug 2019 11:20:48 +0530 Subject: [PATCH 01/12] pyplot.style.use() to accept pathlib.Path objects as arguments --- lib/matplotlib/style/core.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index e151b27b8181..ac3ce14e6456 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, list or Path object A style specification. Valid options are: +------+-------------------------------------------------------------+ @@ -85,12 +85,17 @@ def use(style): | list | A list of style specifiers (str or dict) applied from first | | | to last in the list. | +------+-------------------------------------------------------------+ + | Path | A Path object which is a path to a style file. | + +------+-------------------------------------------------------------+ """ 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. styles = [style] + elif isinstance(style, Path): + # If the style is pathlib.Path object make a single element list of string + styles = [str(style)] else: styles = style From fd3303e00115088daa26ad4bafa513953f4514e1 Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Wed, 28 Aug 2019 11:57:15 +0530 Subject: [PATCH 02/12] Removed the Flake8 error --- lib/matplotlib/style/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index ac3ce14e6456..4a84d3d8a52d 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -94,7 +94,8 @@ def use(style): # If name is a single str or dict, make it a single element list. styles = [style] elif isinstance(style, Path): - # If the style is pathlib.Path object make a single element list of string + # If the style is pathlib.Path object cast to string. + # and make it a single element list. styles = [str(style)] else: styles = style From c9aff7447298ae49c0d078a34a7366894b05e6ef Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Thu, 29 Aug 2019 10:18:21 +0530 Subject: [PATCH 03/12] List to support Path like objects in pyplot.style.use() --- lib/matplotlib/style/core.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 4a84d3d8a52d..92427948662c 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -82,28 +82,26 @@ 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 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) or hasattr(style, 'keys') or \ + isinstance(style, Path): + # If name is a single str, Path or dict, make it a single element list. styles = [style] - elif isinstance(style, Path): - # If the style is pathlib.Path object cast to string. - # and make it a single element list. - styles = [str(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) and not isinstance(style, Path): _apply_style(style) elif style == 'default': # Deprecation warnings were already handled when creating @@ -113,6 +111,8 @@ def use(style): elif style in library: _apply_style(library[style]) else: + if isinstance(style, Path): + style = str(style) try: rc = rc_params_from_file(style, use_default_template=False) _apply_style(rc) From 6f381a420421322470f50d3230d52dfc0d250e99 Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Fri, 30 Aug 2019 03:52:55 +0530 Subject: [PATCH 04/12] Removed the explicit typecaste of Path to str for plt.style.use(). Updated code and comments according to reviews --- lib/matplotlib/__init__.py | 2 +- lib/matplotlib/style/core.py | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index b7cac2a11fa1..3f10df5f07df 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, Path 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 92427948662c..98e26eee9132 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -72,7 +72,7 @@ def use(style): Parameters ---------- - style : str, dict, list or Path object + style : str, dict, Path or list object A style specification. Valid options are: +------+-------------------------------------------------------------+ @@ -91,8 +91,7 @@ def use(style): """ style_alias = {'mpl20': 'default', 'mpl15': 'classic'} - if isinstance(style, str) or hasattr(style, 'keys') or \ - isinstance(style, Path): + 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: @@ -101,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) and not isinstance(style, Path): + if not isinstance(style, (str, Path)): _apply_style(style) elif style == 'default': # Deprecation warnings were already handled when creating @@ -111,8 +110,6 @@ def use(style): elif style in library: _apply_style(library[style]) else: - if isinstance(style, Path): - style = str(style) try: rc = rc_params_from_file(style, use_default_template=False) _apply_style(rc) @@ -129,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: +------+-------------------------------------------------------------+ @@ -139,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 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 From 0f3b9404884036833a91c9171594411478bb5bc2 Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Mon, 2 Sep 2019 10:13:59 +0530 Subject: [PATCH 05/12] Added the test for Path in plt.style.use() --- lib/matplotlib/tests/test_style.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/matplotlib/tests/test_style.py b/lib/matplotlib/tests/test_style.py index 552b87e4aa14..86048178f37a 100644 --- a/lib/matplotlib/tests/test_style.py +++ b/lib/matplotlib/tests/test_style.py @@ -67,6 +67,15 @@ def test_use_url(tmpdir): with style.context(url): assert mpl.rcParams['axes.facecolor'] == "#adeade" +def test_single_path(tmpdir): + mpl.rcParams[PARAM] = 'gray' + temp_file = '%s.%s' % ('test', STYLE_EXTENSION) + path = Path(tmpdir, temp_file) + path.write_text("{} : {}".format(PARAM,VALUE)) + with style.context(path): + assert mpl.rcParams[PARAM] == VALUE + assert mpl.rcParams[PARAM] == 'gray' + def test_context(): mpl.rcParams[PARAM] = 'gray' From a22e16c60a1085d322b7f1b11f3e9aea81d9566c Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Mon, 2 Sep 2019 11:06:21 +0530 Subject: [PATCH 06/12] Removed flake8 errors --- lib/matplotlib/tests/test_style.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_style.py b/lib/matplotlib/tests/test_style.py index 86048178f37a..3e663656041b 100644 --- a/lib/matplotlib/tests/test_style.py +++ b/lib/matplotlib/tests/test_style.py @@ -67,11 +67,12 @@ def test_use_url(tmpdir): with style.context(url): assert mpl.rcParams['axes.facecolor'] == "#adeade" + def test_single_path(tmpdir): mpl.rcParams[PARAM] = 'gray' temp_file = '%s.%s' % ('test', STYLE_EXTENSION) path = Path(tmpdir, temp_file) - path.write_text("{} : {}".format(PARAM,VALUE)) + path.write_text("{} : {}".format(PARAM, VALUE)) with style.context(path): assert mpl.rcParams[PARAM] == VALUE assert mpl.rcParams[PARAM] == 'gray' From 02366445254933e1231b8a624c65a867cd15359f Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Mon, 2 Sep 2019 16:44:29 +0530 Subject: [PATCH 07/12] Changing Path -> pathlib.Path in comments --- lib/matplotlib/__init__.py | 2 +- lib/matplotlib/style/core.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 3f10df5f07df..7e759ec632ab 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, Path + fname : str, pathlib.Path 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 98e26eee9132..482d08e3c01f 100644 --- a/lib/matplotlib/style/core.py +++ b/lib/matplotlib/style/core.py @@ -72,7 +72,7 @@ def use(style): Parameters ---------- - style : str, dict, Path or list object + style : str, dict, pathlib.Path or list A style specification. Valid options are: +------+-------------------------------------------------------------+ From b85a0edf80a4f8cd4f39241dc1ffc192d05e55a7 Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Wed, 4 Sep 2019 10:01:36 +0530 Subject: [PATCH 08/12] Comments according to path-like --- lib/matplotlib/style/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/style/core.py b/lib/matplotlib/style/core.py index 482d08e3c01f..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, pathlib.Path or list + style : str, dict, Path or list A style specification. Valid options are: +------+-------------------------------------------------------------+ @@ -82,7 +82,7 @@ def use(style): | dict | Dictionary with valid key/value pairs for | | | `matplotlib.rcParams`. | +------+-------------------------------------------------------------+ - | Path | A Path object which is a path to a style file. | + | 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. | @@ -136,7 +136,7 @@ def context(style, after_reset=False): | dict | Dictionary with valid key/value pairs for | | | `matplotlib.rcParams`. | +------+-------------------------------------------------------------+ - | Path | A Path object which is a path to a style file. | + | 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. | From 2ea5377997bc394540d384809e5e071b753d8abe Mon Sep 17 00:00:00 2001 From: omsitapara23 Date: Wed, 4 Sep 2019 10:59:02 +0530 Subject: [PATCH 09/12] Added path-like to init.py --- lib/matplotlib/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 7e759ec632ab..d268f67f0359 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, pathlib.Path + fname : str, 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. From 643b08cc1c5fbd4743c9123a8dbdeda7f7581dbd Mon Sep 17 00:00:00 2001 From: Om Sitapara Date: Sat, 7 Sep 2019 15:38:11 +0530 Subject: [PATCH 10/12] Update lib/matplotlib/tests/test_style.py Co-Authored-By: Elliott Sales de Andrade --- lib/matplotlib/tests/test_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_style.py b/lib/matplotlib/tests/test_style.py index 3e663656041b..3af8b128dc3a 100644 --- a/lib/matplotlib/tests/test_style.py +++ b/lib/matplotlib/tests/test_style.py @@ -70,7 +70,7 @@ def test_use_url(tmpdir): def test_single_path(tmpdir): mpl.rcParams[PARAM] = 'gray' - temp_file = '%s.%s' % ('test', STYLE_EXTENSION) + temp_file = f'text.{STYLE_EXTENSION}' path = Path(tmpdir, temp_file) path.write_text("{} : {}".format(PARAM, VALUE)) with style.context(path): From 9db17dfda7d92fbc72b0f96abeee1a3fdfbbbbba Mon Sep 17 00:00:00 2001 From: Om Sitapara Date: Sat, 7 Sep 2019 15:38:18 +0530 Subject: [PATCH 11/12] Update lib/matplotlib/tests/test_style.py Co-Authored-By: Elliott Sales de Andrade --- lib/matplotlib/tests/test_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_style.py b/lib/matplotlib/tests/test_style.py index 3af8b128dc3a..0accfa005f5f 100644 --- a/lib/matplotlib/tests/test_style.py +++ b/lib/matplotlib/tests/test_style.py @@ -72,7 +72,7 @@ def test_single_path(tmpdir): mpl.rcParams[PARAM] = 'gray' temp_file = f'text.{STYLE_EXTENSION}' path = Path(tmpdir, temp_file) - path.write_text("{} : {}".format(PARAM, VALUE)) + path.write_text(f'{PARAM} : {VALUE}') with style.context(path): assert mpl.rcParams[PARAM] == VALUE assert mpl.rcParams[PARAM] == 'gray' From 0c3a7eaf6f5831643acf1082418fedae8acb5d75 Mon Sep 17 00:00:00 2001 From: Om Sitapara Date: Mon, 9 Sep 2019 00:58:32 +0530 Subject: [PATCH 12/12] Update lib/matplotlib/__init__.py changed the comments according to numpydoc Co-Authored-By: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index d268f67f0359..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, path-like + 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.