Skip to content

Commit 7c688ec

Browse files
committed
Use divergent colormap if lowest and highest level span 0
Fixes pydata#3524
1 parent 1416d5a commit 7c688ec

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Bug fixes
4848
- Fix a regression where deleting a coordinate from a copied :py:class:`DataArray`
4949
can affect the original :py:class:`Dataarray`. (:issue:`3899`, :pull:`3871`)
5050
By `Todd Jennings <https://github.com/toddrjen>`_
51+
- Use divergent colormap if ``levels`` spans 0. (:issue:`3524`)
52+
By `Deepak Cherian <https://github.com/dcherian>`_
5153

5254

5355
Documentation

xarray/plot/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,13 @@ def _determine_cmap_params(
216216
vlim = abs(vmax - center)
217217

218218
if possibly_divergent:
219+
levels_are_divergent = (
220+
isinstance(levels, Iterable) and levels[0] * levels[-1] < 0
221+
)
219222
# kwargs not specific about divergent or not: infer defaults from data
220-
divergent = ((vmin < 0) and (vmax > 0)) or not center_is_none
223+
divergent = (
224+
((vmin < 0) and (vmax > 0)) or not center_is_none or levels_are_divergent
225+
)
221226
else:
222227
divergent = False
223228

xarray/tests/test_plot.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,12 @@ def test_divergentcontrol(self):
827827
assert cmap_params["vmax"] == 0.6
828828
assert cmap_params["cmap"] == "viridis"
829829

830+
# regression test for GH3524
831+
# infer diverging colormap from divergent levels
832+
cmap_params = _determine_cmap_params(pos, levels=[-0.1, 0, 1])
833+
# specifying levels makes cmap a Colormap object
834+
assert cmap_params["cmap"].name == "RdBu_r"
835+
830836
def test_norm_sets_vmin_vmax(self):
831837
vmin = self.data.min()
832838
vmax = self.data.max()

0 commit comments

Comments
 (0)