diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 6580695adaf..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 ~~~~~~~~~~~~~ diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index b5d5a122c7a..e807081f838 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -304,7 +304,7 @@ 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) and (not isinstance(norm, mpl.colors.BoundaryNorm)): cmap, newnorm = _build_discrete_cmap(cmap, levels, extend, filled) norm = newnorm if norm is None else norm diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index b7b5f005f0c..02f7f4b9be2 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1204,6 +1204,13 @@ 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: + 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 + class Common2dMixin: """