Skip to content

facetgrid: Ensure that colormap params are only determined once. #3915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,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 @@ -735,6 +736,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 @@ -781,6 +783,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"])