Folding color palettes

Is there an easy way to create a new color map by combining two existing ones?

What I'm trying to achieve is to make another color scatter plot where the variable with the color display varies from large negative to large positive values, and I would like to soften the values ​​around zero - - basically, I would like to be able to choose colors from a color map (for example cm.Blues_r) for negative values ​​of a variable that has been changed in color and from another (for example, cm.Oranges) for positive values ​​this variable.

+2
source share
2 answers

, colors.Colormap.

class split_cmap(colors.Colormap):
    def __init__(self, cmap_a, cmap_b, split=.5):
        '''Makes a split color map cmap_a is the low range, 
           cmap_b is the high range
           split is where to break the range
        '''
        self.cmap_a, self.cmap_b = cmap_a, cmap_b
        self.split = split

    def __call__(self, v):
        if v < self.split:
            return self.cmap_a(v) 
            # or you might want to use v / self.split
        else:
            return self.cmap_b(v) 
            # or you might want to use (v - self.split) / (1 - self.split)

    def set_bad(self,*args, **kwargs):
        self.cmap_a.set_bad(*args, **kwargs)
        self.cmap_b.set_bad(*args, **kwargs)

    def set_over(self, *args, **kwargs):
        self.cmap_a.set_over(*args, **kwargs) # not really needed
        self.cmap_b.set_over(*args, **kwargs)

    def set_under(self, *args, **kwargs):
        self.cmap_a.set_under(*args, **kwargs)
        self.cmap_b.set_under(*args, **kwargs) # not really needed

    def is_gray(self):
        return False

colors.Colormap .

Normalize. [0, 1], , norm .5, , .

, , , . .

, , .

+1

, , . --.

cdict = {'red':   [ (0.0,   0.0, 0.0),
                    (0.475, 1.0, 1.0),
                    (0.525, 1.0, 1.0),
                    (1.0,   1.0, 1.0)
                  ],
         'green': [ (0.0,   0.0, 0.0),
                    (0.475, 1.0, 1.0),
                    (0.525, 1.0, 1.0),
                    (1.0,   0.65, 0.0)
                  ],
         'blue':  [ (0.0,   1.0, 1.0),
                    (0.475, 1.0, 1.0),
                    (0.525, 1.0, 1.0),
                    (1.0,   0.0, 0.0)
                  ]
}
rwb_cmap = matplotlib.colors.LinearSegmentedColormap(name = 'rwb_colormap', colors = cdict, N = 256)

Colormap - RGB. . z 0 1. .

segment z-axis  end      start
i       z[i]    v0[i]    v1[i]
i+1     z[i+1]  v0[i+1]  v1[i+1]   
i+2     z[i+2]  v0[i+2]  v1[i+2]   

z[i] z[i+1] v1[i] v0[i+1] .. "" . v0[0] v1[-1] . , . (: http://matplotlib.org/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap)

N - . , N = 256 256 . 256 . , , N = 6, 4 .

0,475 0,525 , . [-1.5, -0.5, 0.5, 1.5] --. 0,5 , .

RGB 255-165-0 1-0,65-0, 0-1.

0

Source: https://habr.com/ru/post/1618681/


All Articles