From 0a288a2c908beb3e3f87cd0f7c5e89712b08a991 Mon Sep 17 00:00:00 2001 From: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:27:56 +0100 Subject: [PATCH 01/12] Update utils.py Proposing update to fix https://github.com/pydata/xarray/issues/7014 --- xarray/plot/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index b5d5a122c7a..158e74014f8 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -304,9 +304,11 @@ def _determine_cmap_params( if extend is None: extend = _determine_extend(calc_data, vmin, vmax) - if levels is not None or isinstance(norm, mpl.colors.BoundaryNorm): + if levels is not None: cmap, newnorm = _build_discrete_cmap(cmap, levels, extend, filled) norm = newnorm if norm is None else norm + if isinstance(norm, mpl.colors.BoundaryNorm): + cmap, norm = _build_discrete_cmap(cmap, levels, extend, filled) # vmin & vmax needs to be None if norm is passed # TODO: always return a norm with vmin and vmax From 3d84b2c43a2c31d3bf47447858f46666347b33ae Mon Sep 17 00:00:00 2001 From: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Date: Thu, 23 Feb 2023 16:27:27 +0100 Subject: [PATCH 02/12] Update test_plot.py added testcase for https://github.com/pydata/xarray/issues/7014 --- xarray/tests/test_plot.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 40204691e85..43fc2baa9b8 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1193,6 +1193,11 @@ def test_discrete_colormap_provided_boundary_norm(self) -> None: norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) primitive = self.darray.plot.contourf(norm=norm) np.testing.assert_allclose(primitive.levels, norm.boundaries) + + def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self): + norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) + primitive = self.darray.plot.contourf(norm=norm) + assert primitive.colorbar.norm.Ncmap+1 == primitive.colorbar.norm.N class Common2dMixin: From 4effc03a1368d88b0cef4b51b0814c72c067ea57 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:58:26 +0000 Subject: [PATCH 03/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 43fc2baa9b8..61c50eaec23 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1193,11 +1193,11 @@ def test_discrete_colormap_provided_boundary_norm(self) -> None: norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) primitive = self.darray.plot.contourf(norm=norm) np.testing.assert_allclose(primitive.levels, norm.boundaries) - + def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self): norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) primitive = self.darray.plot.contourf(norm=norm) - assert primitive.colorbar.norm.Ncmap+1 == primitive.colorbar.norm.N + assert primitive.colorbar.norm.Ncmap + 1 == primitive.colorbar.norm.N class Common2dMixin: From c2ce3c19d74bba9af8d9f164f4b3ffe6f130e423 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Fri, 10 Mar 2023 16:51:58 +0100 Subject: [PATCH 04/12] fix as suggested by jklymak --- xarray/plot/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 158e74014f8..e807081f838 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -304,11 +304,9 @@ def _determine_cmap_params( if extend is None: extend = _determine_extend(calc_data, vmin, vmax) - if levels is not None: + if (levels is not None) and (not isinstance(norm, mpl.colors.BoundaryNorm)): cmap, newnorm = _build_discrete_cmap(cmap, levels, extend, filled) norm = newnorm if norm is None else norm - if isinstance(norm, mpl.colors.BoundaryNorm): - cmap, norm = _build_discrete_cmap(cmap, levels, extend, filled) # vmin & vmax needs to be None if norm is passed # TODO: always return a norm with vmin and vmax From 1c764c229928d9965b96a47eb86f8a2552f78e8a Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Fri, 10 Mar 2023 17:00:10 +0100 Subject: [PATCH 05/12] updated testcase --- xarray/tests/test_plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 61c50eaec23..3f420c012c4 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1197,7 +1197,7 @@ def test_discrete_colormap_provided_boundary_norm(self) -> None: def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self): norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) primitive = self.darray.plot.contourf(norm=norm) - assert primitive.colorbar.norm.Ncmap + 1 == primitive.colorbar.norm.N + assert primitive.colorbar.norm.Ncmap == primitive.colorbar.norm.N class Common2dMixin: From 3a60f1d390d8d6eff3dc91c5db707d19fb9f789c Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Thu, 16 Mar 2023 13:20:33 +0100 Subject: [PATCH 06/12] enabled type checking of test_discrete_colormap_provided_boundary_norm_matching_cmap_levels() --- xarray/tests/test_plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 3f420c012c4..c18550ae0d5 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1194,7 +1194,7 @@ def test_discrete_colormap_provided_boundary_norm(self) -> None: primitive = self.darray.plot.contourf(norm=norm) np.testing.assert_allclose(primitive.levels, norm.boundaries) - def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self): + def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self) -> None: norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) primitive = self.darray.plot.contourf(norm=norm) assert primitive.colorbar.norm.Ncmap == primitive.colorbar.norm.N From 317cab8c9e29644fe0b1f70ac0e28b3f367ccd0f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Mar 2023 12:21:32 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_plot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index c18550ae0d5..2dd109cd896 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1194,7 +1194,9 @@ def test_discrete_colormap_provided_boundary_norm(self) -> None: primitive = self.darray.plot.contourf(norm=norm) np.testing.assert_allclose(primitive.levels, norm.boundaries) - def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self) -> None: + def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels( + self, + ) -> None: norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) primitive = self.darray.plot.contourf(norm=norm) assert primitive.colorbar.norm.Ncmap == primitive.colorbar.norm.N From 5b5335dbe305aea21abf05b3b5a882e45394a1a1 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Thu, 16 Mar 2023 13:28:56 +0100 Subject: [PATCH 08/12] updated whats-new.rst --- doc/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 0266b2dc0f5..b130336c17b 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -46,6 +46,8 @@ Deprecations Bug fixes ~~~~~~~~~ +- Proper plotting with BoundaryNorm type argument in :py:meth:`DataArray.plot(norm=matplotlib.colors.BoundaryNorm)`. (:issue:`4061`, :issue:`7014`,:pull:`7553`) + By `Jelmer Veenstra `_. - Improve error message when using in :py:meth:`Dataset.drop_vars` to state which variables can't be dropped. (:pull:`7518`) By `Tom Nicholas `_. - Require to explicitly defining optional dimensions such as hue From b6d6cb1c8ac067feb9516f739336cbbc1b757548 Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Thu, 16 Mar 2023 13:31:58 +0100 Subject: [PATCH 09/12] enabled type checking of test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(), again --- xarray/tests/test_plot.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 2dd109cd896..c18550ae0d5 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1194,9 +1194,7 @@ def test_discrete_colormap_provided_boundary_norm(self) -> None: primitive = self.darray.plot.contourf(norm=norm) np.testing.assert_allclose(primitive.levels, norm.boundaries) - def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels( - self, - ) -> None: + def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self) -> None: norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) primitive = self.darray.plot.contourf(norm=norm) assert primitive.colorbar.norm.Ncmap == primitive.colorbar.norm.N From e7306a0208193a18e3cd6016c3c90705df238e9e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 16 Mar 2023 12:35:16 +0000 Subject: [PATCH 10/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_plot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 1ce2bfc6fbf..20bb6b81044 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1194,7 +1194,9 @@ def test_discrete_colormap_provided_boundary_norm(self) -> None: primitive = self.darray.plot.contourf(norm=norm) np.testing.assert_allclose(primitive.levels, norm.boundaries) - def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self) -> None: + def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels( + self, + ) -> None: norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4) primitive = self.darray.plot.contourf(norm=norm) assert primitive.colorbar.norm.Ncmap == primitive.colorbar.norm.N From 4dfb3ac162af9b70c6b1246949e7dbb0e9a6960f Mon Sep 17 00:00:00 2001 From: veenstrajelmer <60435591+veenstrajelmer@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:58:31 +0200 Subject: [PATCH 11/12] Update doc/whats-new.rst Co-authored-by: Deepak Cherian --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 51c42a4f21c..0dafaf183fe 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -49,7 +49,7 @@ Deprecations Bug fixes ~~~~~~~~~ -- Proper plotting with BoundaryNorm type argument in :py:meth:`DataArray.plot(norm=matplotlib.colors.BoundaryNorm)`. (:issue:`4061`, :issue:`7014`,:pull:`7553`) +- Proper plotting when passing :py:class:`~matplotlib.colors.BoundaryNorm` type argument in :py:meth:`DataArray.plot`. (:issue:`4061`, :issue:`7014`,:pull:`7553`) By `Jelmer Veenstra `_. - Improve error message when using in :py:meth:`Dataset.drop_vars` to state which variables can't be dropped. (:pull:`7518`) By `Tom Nicholas `_. From b3a7c4a913834ddc5d994a9e62959bb9ca50329e Mon Sep 17 00:00:00 2001 From: veenstrajelmer Date: Mon, 27 Mar 2023 12:03:04 +0200 Subject: [PATCH 12/12] moved whatsnew for pull 7553 to 2023.04 section --- doc/whats-new.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 5ee8974e560..a98889c225e 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -39,6 +39,8 @@ Bug fixes By `Shreyal Gupta `_ and `Michael Niklas `_. - Improve error message when trying to open a file which you do not have permission to read (:issue:`6523`, :pull:`7629`). By `Thomas Coleman `_. +- Proper plotting when passing :py:class:`~matplotlib.colors.BoundaryNorm` type argument in :py:meth:`DataArray.plot`. (:issue:`4061`, :issue:`7014`,:pull:`7553`) + By `Jelmer Veenstra `_. Documentation ~~~~~~~~~~~~~ @@ -89,8 +91,6 @@ Deprecations Bug fixes ~~~~~~~~~ -- Proper plotting when passing :py:class:`~matplotlib.colors.BoundaryNorm` type argument in :py:meth:`DataArray.plot`. (:issue:`4061`, :issue:`7014`,:pull:`7553`) - By `Jelmer Veenstra `_. - Improve error message when using in :py:meth:`Dataset.drop_vars` to state which variables can't be dropped. (:pull:`7518`) By `Tom Nicholas `_. - Require to explicitly defining optional dimensions such as hue