Skip to content

Commit 97f3a74

Browse files
Switch to use cmap.with_extremes (#10836)
* Switch to set_extremes, set_bad is deprecated * Use with_extremes * Update test_plot.py * Use with_extremes * simplify, np.nan returns the same as masked_invalid now --------- Co-authored-by: Kai Mühlbauer <[email protected]>
1 parent 7f57f01 commit 97f3a74

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

xarray/plot/utils.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,27 +100,22 @@ def _build_discrete_cmap(cmap, levels, extend, filled):
100100

101101
# copy colors to use for bad, under, and over values in case they have been
102102
# set to non-default values
103-
try:
104-
# matplotlib<3.2 only uses bad color for masked values
105-
bad = cmap(np.ma.masked_invalid([np.nan]))[0]
106-
except TypeError:
107-
# cmap was a str or list rather than a color-map object, so there are
108-
# no bad, under or over values to check or copy
109-
pass
110-
else:
111-
under = cmap(-np.inf)
112-
over = cmap(np.inf)
113-
114-
new_cmap.set_bad(bad)
103+
if isinstance(cmap, mpl.colors.Colormap):
104+
bad = cmap(np.nan)
115105

116106
# Only update under and over if they were explicitly changed by the user
117107
# (i.e. are different from the lowest or highest values in cmap). Otherwise
118108
# leave unchanged so new_cmap uses its default values (its own lowest and
119109
# highest values).
120-
if under != cmap(0):
121-
new_cmap.set_under(under)
122-
if over != cmap(cmap.N - 1):
123-
new_cmap.set_over(over)
110+
under = cmap(-np.inf)
111+
if under == cmap(0):
112+
under = None
113+
114+
over = cmap(np.inf)
115+
if over == cmap(cmap.N - 1):
116+
over = None
117+
118+
new_cmap = new_cmap.with_extremes(bad=bad, under=under, over=over)
124119

125120
return new_cmap, cnorm
126121

xarray/tests/test_plot.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,21 +482,20 @@ def test_contourf_cmap_set(self) -> None:
482482
def test_contourf_cmap_set_with_bad_under_over(self) -> None:
483483
a = DataArray(easy_array((4, 4)), dims=["z", "time"])
484484

485-
# make a copy here because we want a local cmap that we will modify.
486-
cmap_expected = copy(mpl.colormaps["viridis"])
485+
# make a copy using with_extremes because we want a local cmap:
486+
cmap_expected = mpl.colormaps["viridis"].with_extremes(
487+
bad="w", under="r", over="g"
488+
)
487489

488-
cmap_expected.set_bad("w")
489490
# check we actually changed the set_bad color
490491
assert np.all(
491492
cmap_expected(np.ma.masked_invalid([np.nan]))[0]
492493
!= mpl.colormaps["viridis"](np.ma.masked_invalid([np.nan]))[0]
493494
)
494495

495-
cmap_expected.set_under("r")
496496
# check we actually changed the set_under color
497497
assert cmap_expected(-np.inf) != mpl.colormaps["viridis"](-np.inf)
498498

499-
cmap_expected.set_over("g")
500499
# check we actually changed the set_over color
501500
assert cmap_expected(np.inf) != mpl.colormaps["viridis"](-np.inf)
502501

0 commit comments

Comments
 (0)