Skip to content

Commit 2c77eb5

Browse files
authored
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
1 parent 745658b commit 2c77eb5

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ Bug fixes
6666
- Fix a regression where deleting a coordinate from a copied :py:class:`DataArray`
6767
can affect the original :py:class:`Dataarray`. (:issue:`3899`, :pull:`3871`)
6868
By `Todd Jennings <https://github.com/toddrjen>`_
69+
- Fix :py:class:`~xarray.plot.FacetGrid` plots with a single contour. (:issue:`3569`, :pull:`3915`).
70+
By `Deepak Cherian <https://github.com/dcherian>`_
6971
- Use divergent colormap if ``levels`` spans 0. (:issue:`3524`)
7072
By `Deepak Cherian <https://github.com/dcherian>`_
71-
- Fix ``FacetGrid`` when ``vmin == vmax``. (:issue:`3734`)
73+
- Fix :py:class:`~xarray.plot.FacetGrid` when ``vmin == vmax``. (:issue:`3734`)
7274
By `Deepak Cherian <https://github.com/dcherian>`_
7375
- Fix bug where plotting line plots with 2D coordinates depended on dimension
7476
order. (:issue:`3933`)

xarray/plot/facetgrid.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ def map_dataarray(self, func, x, y, **kwargs):
273273
# None is the sentinel value
274274
if d is not None:
275275
subset = self.data.loc[d]
276-
mappable = func(subset, x=x, y=y, ax=ax, **func_kwargs)
276+
mappable = func(
277+
subset, x=x, y=y, ax=ax, **func_kwargs, _is_facetgrid=True
278+
)
277279
self._mappables.append(mappable)
278280

279281
self._finalize_grid(x, y)

xarray/plot/plot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,10 @@ def newplotfunc(
693693
_ensure_plottable(xplt, yplt, zval)
694694

695695
cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
696-
plotfunc, zval.data, **locals()
696+
plotfunc,
697+
zval.data,
698+
**locals(),
699+
_is_facetgrid=kwargs.pop("_is_facetgrid", False),
697700
)
698701

699702
if "contour" in plotfunc.__name__:

xarray/plot/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def _determine_cmap_params(
153153
levels=None,
154154
filled=True,
155155
norm=None,
156+
_is_facetgrid=False,
156157
):
157158
"""
158159
Use some heuristics to set good defaults for colorbar and range.
@@ -736,6 +737,7 @@ def _process_cmap_cbar_kwargs(
736737
colors=None,
737738
cbar_kwargs: Union[Iterable[Tuple[str, Any]], Mapping[str, Any]] = None,
738739
levels=None,
740+
_is_facetgrid=False,
739741
**kwargs,
740742
):
741743
"""
@@ -782,6 +784,12 @@ def _process_cmap_cbar_kwargs(
782784

783785
cmap_args = getfullargspec(_determine_cmap_params).args
784786
cmap_kwargs.update((a, kwargs[a]) for a in cmap_args if a in kwargs)
785-
cmap_params = _determine_cmap_params(**cmap_kwargs)
787+
if not _is_facetgrid:
788+
cmap_params = _determine_cmap_params(**cmap_kwargs)
789+
else:
790+
cmap_params = {
791+
k: cmap_kwargs[k]
792+
for k in ["vmin", "vmax", "cmap", "extend", "levels", "norm"]
793+
}
786794

787795
return cmap_params, cbar_kwargs

xarray/tests/test_plot.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,3 +2319,15 @@ def test_plot_transposes_properly(plotfunc):
23192319
# pcolormesh returns 1D array but imshow returns a 2D array so it is necessary
23202320
# to ravel() on the LHS
23212321
assert np.all(hdl.get_array().ravel() == da.to_masked_array().ravel())
2322+
2323+
2324+
@requires_matplotlib
2325+
def test_facetgrid_single_contour():
2326+
# regression test for GH3569
2327+
x, y = np.meshgrid(np.arange(12), np.arange(12))
2328+
z = xr.DataArray(np.sqrt(x ** 2 + y ** 2))
2329+
z2 = xr.DataArray(np.sqrt(x ** 2 + y ** 2) + 1)
2330+
ds = xr.concat([z, z2], dim="time")
2331+
ds["time"] = [0, 1]
2332+
2333+
ds.plot.contour(col="time", levels=[4], colors=["k"])

0 commit comments

Comments
 (0)