From 2c77eb531b6689f9f1d2adbde0d8bf852f1f7362 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Sat, 11 Apr 2020 10:11:51 -0600 Subject: [PATCH] facetgrid: Ensure that colormap params are only determined once. (#3915) * facetgrid: Ensure that colormap params are only determined once. Fixes #3569 * blacken * Add test * update whats-new --- doc/whats-new.rst | 4 +++- xarray/plot/facetgrid.py | 4 +++- xarray/plot/plot.py | 5 ++++- xarray/plot/utils.py | 10 +++++++++- xarray/tests/test_plot.py | 12 ++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 2838e0b8e80..e4c3a4d533f 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -66,9 +66,11 @@ 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 :py:class:`~xarray.plot.FacetGrid` plots with a single contour. (:issue:`3569`, :pull:`3915`). + By `Deepak Cherian `_ - Use divergent colormap if ``levels`` spans 0. (:issue:`3524`) By `Deepak Cherian `_ -- Fix ``FacetGrid`` when ``vmin == vmax``. (:issue:`3734`) +- Fix :py:class:`~xarray.plot.FacetGrid` when ``vmin == vmax``. (:issue:`3734`) By `Deepak Cherian `_ - Fix bug where plotting line plots with 2D coordinates depended on dimension order. (:issue:`3933`) diff --git a/xarray/plot/facetgrid.py b/xarray/plot/facetgrid.py index 4f3268c1203..819eded694e 100644 --- a/xarray/plot/facetgrid.py +++ b/xarray/plot/facetgrid.py @@ -273,7 +273,9 @@ def map_dataarray(self, func, x, y, **kwargs): # None is the sentinel value if d is not None: subset = self.data.loc[d] - mappable = func(subset, x=x, y=y, ax=ax, **func_kwargs) + mappable = func( + subset, x=x, y=y, ax=ax, **func_kwargs, _is_facetgrid=True + ) self._mappables.append(mappable) self._finalize_grid(x, y) diff --git a/xarray/plot/plot.py b/xarray/plot/plot.py index 0fe302833d4..4657bee9415 100644 --- a/xarray/plot/plot.py +++ b/xarray/plot/plot.py @@ -693,7 +693,10 @@ def newplotfunc( _ensure_plottable(xplt, yplt, zval) cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs( - plotfunc, zval.data, **locals() + plotfunc, + zval.data, + **locals(), + _is_facetgrid=kwargs.pop("_is_facetgrid", False), ) if "contour" in plotfunc.__name__: diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index fb744e43fc3..c3512828888 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -153,6 +153,7 @@ def _determine_cmap_params( levels=None, filled=True, norm=None, + _is_facetgrid=False, ): """ Use some heuristics to set good defaults for colorbar and range. @@ -736,6 +737,7 @@ def _process_cmap_cbar_kwargs( colors=None, cbar_kwargs: Union[Iterable[Tuple[str, Any]], Mapping[str, Any]] = None, levels=None, + _is_facetgrid=False, **kwargs, ): """ @@ -782,6 +784,12 @@ def _process_cmap_cbar_kwargs( cmap_args = getfullargspec(_determine_cmap_params).args cmap_kwargs.update((a, kwargs[a]) for a in cmap_args if a in kwargs) - cmap_params = _determine_cmap_params(**cmap_kwargs) + if not _is_facetgrid: + cmap_params = _determine_cmap_params(**cmap_kwargs) + else: + cmap_params = { + k: cmap_kwargs[k] + for k in ["vmin", "vmax", "cmap", "extend", "levels", "norm"] + } return cmap_params, cbar_kwargs diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index d0a8a02a3a4..bf1f9ed60bb 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -2319,3 +2319,15 @@ def test_plot_transposes_properly(plotfunc): # pcolormesh returns 1D array but imshow returns a 2D array so it is necessary # to ravel() on the LHS assert np.all(hdl.get_array().ravel() == da.to_masked_array().ravel()) + + +@requires_matplotlib +def test_facetgrid_single_contour(): + # regression test for GH3569 + x, y = np.meshgrid(np.arange(12), np.arange(12)) + z = xr.DataArray(np.sqrt(x ** 2 + y ** 2)) + z2 = xr.DataArray(np.sqrt(x ** 2 + y ** 2) + 1) + ds = xr.concat([z, z2], dim="time") + ds["time"] = [0, 1] + + ds.plot.contour(col="time", levels=[4], colors=["k"])