Closed
Description
xarray
's plotting functionality sometimes needs to turn a colormap into a discrete colormap. I came across this in fixing a bug pydata/xarray#3601 where xarray
was ignoring the effects of set_under()
, etc., which had been called on the colormap. We've fixed that problem, but ended up with this https://github.com/pydata/xarray/blob/ae6609b025d62a0d863869cbd16db167a8855167/xarray/plot/utils.py#L94-L105
n_colors = len(levels) + ext_n - 1
pal = _color_palette(cmap, n_colors)
new_cmap, cnorm = mpl.colors.from_levels_and_colors(levels, pal, extend=extend)
# copy the old cmap name, for easier testing
new_cmap.name = getattr(cmap, "name", cmap)
# copy colors to use for bad, under, and over values in case they have been set to
# non-default values
new_cmap._rgba_bad = getattr(cmap, "_rgba_bad", new_cmap._rgba_bad)
new_cmap._rgba_under = getattr(cmap, "_rgba_under", new_cmap._rgba_under)
new_cmap._rgba_over = getattr(cmap, "_rgba_over", new_cmap._rgba_over)
This works, but is not particularly elegant, and if there are any other properties of the original cmap
that we want to keep, we'd have to copy them over explicitly. @dcherian asked me to see if anyone on here could suggest a better way of modifying a cmap
into a discrete cmap
?