Skip to content

Commit 47a6fc0

Browse files
committed
Only change under, over colors if they were set by user
When modifying a colormap, we only want to preserve the under and over colors of the original colormap if they were explicitly set by the user. In _build_discrete_cmap this makes no difference, as the new_cmap returned by mpl.colors.from_levels_and_colors has the same minimum and maximum colors as its input, so the default under and over colors would not change anyway and could be copied regardless. However, for clarity and in case the same pattern is needed in future elsewhere, it is nicer to add a checks for: whether the under color is the same as cmap(0), only setting under for new_cmap if it is not; and whether the over color is the same as cmap(cmap.N - 1), only setting over for new_cmap if it is not.
1 parent de6ffeb commit 47a6fc0

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

xarray/plot/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,15 @@ def _build_discrete_cmap(cmap, levels, extend, filled):
112112
over = cmap(np.inf)
113113

114114
new_cmap.set_bad(bad)
115-
new_cmap.set_under(under)
116-
new_cmap.set_over(over)
115+
116+
# Only update under and over if they were explicitly changed by the user
117+
# (i.e. are different from the lowest or highest values in cmap). Otherwise
118+
# leave unchanged so new_cmap uses its default values (its own lowest and
119+
# 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)
117124

118125
return new_cmap, cnorm
119126

0 commit comments

Comments
 (0)