Skip to content

Commit

Permalink
facetgrid: Ensure that colormap params are only determined once. (#3915)
Browse files Browse the repository at this point in the history
* facetgrid: Ensure that colormap params are only determined once.

Fixes #3569

* blacken

* Add test

* update whats-new
  • Loading branch information
dcherian authored Apr 11, 2020
1 parent 745658b commit 2c77eb5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/toddrjen>`_
- Fix :py:class:`~xarray.plot.FacetGrid` plots with a single contour. (:issue:`3569`, :pull:`3915`).
By `Deepak Cherian <https://github.com/dcherian>`_
- Use divergent colormap if ``levels`` spans 0. (:issue:`3524`)
By `Deepak Cherian <https://github.com/dcherian>`_
- Fix ``FacetGrid`` when ``vmin == vmax``. (:issue:`3734`)
- Fix :py:class:`~xarray.plot.FacetGrid` when ``vmin == vmax``. (:issue:`3734`)
By `Deepak Cherian <https://github.com/dcherian>`_
- Fix bug where plotting line plots with 2D coordinates depended on dimension
order. (:issue:`3933`)
Expand Down
4 changes: 3 additions & 1 deletion xarray/plot/facetgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__:
Expand Down
10 changes: 9 additions & 1 deletion xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
):
"""
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

0 comments on commit 2c77eb5

Please sign in to comment.