Skip to content

Commit

Permalink
Only change under, over colors if they were set by user
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
johnomotani committed Feb 4, 2020
1 parent de6ffeb commit 47a6fc0
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,15 @@ def _build_discrete_cmap(cmap, levels, extend, filled):
over = cmap(np.inf)

new_cmap.set_bad(bad)
new_cmap.set_under(under)
new_cmap.set_over(over)

# Only update under and over if they were explicitly changed by the user
# (i.e. are different from the lowest or highest values in cmap). Otherwise
# leave unchanged so new_cmap uses its default values (its own lowest and
# highest values).
if under != cmap(0):
new_cmap.set_under(under)
if over != cmap(cmap.N - 1):
new_cmap.set_over(over)

return new_cmap, cnorm

Expand Down

0 comments on commit 47a6fc0

Please sign in to comment.