From bd84186acbd84bd386134a5b60111596cee2d8ec Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Tue, 12 May 2020 22:47:50 +0000 Subject: [PATCH] Fix contour when levels is scalar and norm is provided. (#3914) Fixes #3735 --- doc/whats-new.rst | 2 ++ xarray/plot/utils.py | 2 +- xarray/tests/test_plot.py | 9 +++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a1d52b28ed5..0724460b1e5 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -91,6 +91,8 @@ Bug fixes By `Deepak Cherian `_ - Fix :py:class:`~xarray.plot.FacetGrid` when ``vmin == vmax``. (:issue:`3734`) By `Deepak Cherian `_ +- Fix plotting when ``levels`` is a scalar and ``norm`` is provided. (:issue:`3735`) + By `Deepak Cherian `_ - Fix bug where plotting line plots with 2D coordinates depended on dimension order. (:issue:`3933`) By `Tom Nicholas `_. diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index c3512828888..cb993c192d9 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -268,7 +268,7 @@ def _determine_cmap_params( cmap = OPTIONS["cmap_sequential"] # Handle discrete levels - if levels is not None and norm is None: + if levels is not None: if is_scalar(levels): if user_minmax: levels = np.linspace(vmin, vmax, levels) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index bf1f9ed60bb..af7c686bf60 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -854,21 +854,22 @@ def test_norm_sets_vmin_vmax(self): vmin = self.data.min() vmax = self.data.max() - for norm, extend in zip( + for norm, extend, levels in zip( [ + mpl.colors.Normalize(), mpl.colors.Normalize(), mpl.colors.Normalize(vmin + 0.1, vmax - 0.1), mpl.colors.Normalize(None, vmax - 0.1), mpl.colors.Normalize(vmin + 0.1, None), ], - ["neither", "both", "max", "min"], + ["neither", "neither", "both", "max", "min"], + [7, None, None, None, None], ): test_min = vmin if norm.vmin is None else norm.vmin test_max = vmax if norm.vmax is None else norm.vmax - cmap_params = _determine_cmap_params(self.data, norm=norm) - + cmap_params = _determine_cmap_params(self.data, norm=norm, levels=levels) assert cmap_params["vmin"] == test_min assert cmap_params["vmax"] == test_max assert cmap_params["extend"] == extend