Skip to content

Modifying colormaps (as used in xarray) #16296

@johnomotani

Description

@johnomotani

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy