diff --git a/doc/whats-new.rst b/doc/whats-new.rst index c70dfd4f3f6..1a1585596eb 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -48,7 +48,8 @@ Bug fixes - Fix a regression where deleting a coordinate from a copied :py:class:`DataArray` can affect the original :py:class:`Dataarray`. (:issue:`3899`, :pull:`3871`) By `Todd Jennings `_ - +- Fix ``FacetGrid`` when ``vmin == vmax``. (:issue:`3734`) + By `Deepak Cherian `_ Documentation ~~~~~~~~~~~~~ diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index e6c15037cb8..9fdfcc2c318 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -237,18 +237,14 @@ def _determine_cmap_params( norm.vmin = vmin else: if not vmin_was_none and vmin != norm.vmin: - raise ValueError( - "Cannot supply vmin and a norm" + " with a different vmin." - ) + raise ValueError("Cannot supply vmin and a norm with a different vmin.") vmin = norm.vmin if norm.vmax is None: norm.vmax = vmax else: if not vmax_was_none and vmax != norm.vmax: - raise ValueError( - "Cannot supply vmax and a norm" + " with a different vmax." - ) + raise ValueError("Cannot supply vmax and a norm with a different vmax.") vmax = norm.vmax # if BoundaryNorm, then set levels @@ -275,6 +271,10 @@ def _determine_cmap_params( levels = ticker.tick_values(vmin, vmax) vmin, vmax = levels[0], levels[-1] + # GH3734 + if vmin == vmax: + vmin, vmax = mpl.ticker.LinearLocator(2).tick_values(vmin, vmax) + if extend is None: extend = _determine_extend(calc_data, vmin, vmax) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 7f3f1620133..46556c14517 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -833,10 +833,10 @@ def test_norm_sets_vmin_vmax(self): for norm, extend in zip( [ - mpl.colors.LogNorm(), - mpl.colors.LogNorm(vmin + 1, vmax - 1), - mpl.colors.LogNorm(None, vmax - 1), - mpl.colors.LogNorm(vmin + 1, None), + 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"], ): @@ -1752,6 +1752,13 @@ def test_can_set_vmin_vmax(self): clim = np.array(image.get_clim()) assert np.allclose(expected, clim) + @pytest.mark.slow + def test_vmin_vmax_equal(self): + # regression test for GH3734 + fg = self.g.map_dataarray(xplt.imshow, "x", "y", vmin=50, vmax=50) + for mappable in fg._mappables: + assert mappable.norm.vmin != mappable.norm.vmax + @pytest.mark.slow @pytest.mark.filterwarnings("ignore") def test_can_set_norm(self):