Search code examples
pythoncolormap

Can you reverse the order of a branca colormap?


I have a branca colormap, is it possible flip it's order?

e.g. This: enter image description here

To this: enter image description here

Code:

import branca.colormap as cm
den_colormap = cm.linear.RdBu_11

Solution

  • Try this:

    import branca.colormap as cm
    
    den_colormap = cm.linear.RdBu_11.colors
    print('Original : \n', den_colormap)
    
    den_colormap.reverse()
    print('reversed : \n', den_colormap)
    
    out = cm.LinearColormap(colors=den_colormap)
    print('out : \n', out.colors)
    

    Output:

    Original : 
     [(0.403921568627451, 0.0, 0.12156862745098039, 1.0), (0.6980392156862745, 0.09411764705882353, 0.16862745098039217, 1.0), (0.8392156862745098, 0.3764705882352941, 0.30196078431372547, 1.0), (0.9568627450980393, 0.6470588235294118, 0.5098039215686274, 1.0), (0.9921568627450981, 0.8588235294117647, 0.7803921568627451, 1.0), (0.9686274509803922, 0.9686274509803922, 0.9686274509803922, 1.0), (0.8196078431372549, 0.8980392156862745, 0.9411764705882353, 1.0), (0.5725490196078431, 0.7725490196078432, 0.8705882352941177, 1.0), (0.2627450980392157, 0.5764705882352941, 0.7647058823529411, 1.0), (0.12941176470588237, 0.4, 0.6745098039215687, 1.0), (0.0196078431372549, 0.18823529411764706, 0.3803921568627451, 1.0)]
    
    reversed : 
     [(0.0196078431372549, 0.18823529411764706, 0.3803921568627451, 1.0), (0.12941176470588237, 0.4, 0.6745098039215687, 1.0), (0.2627450980392157, 0.5764705882352941, 0.7647058823529411, 1.0), (0.5725490196078431, 0.7725490196078432, 0.8705882352941177, 1.0), (0.8196078431372549, 0.8980392156862745, 0.9411764705882353, 1.0), (0.9686274509803922, 0.9686274509803922, 0.9686274509803922, 1.0), (0.9921568627450981, 0.8588235294117647, 0.7803921568627451, 1.0), (0.9568627450980393, 0.6470588235294118, 0.5098039215686274, 1.0), (0.8392156862745098, 0.3764705882352941, 0.30196078431372547, 1.0), (0.6980392156862745, 0.09411764705882353, 0.16862745098039217, 1.0), (0.403921568627451, 0.0, 0.12156862745098039, 1.0)]
    
    out : 
     [(0.0196078431372549, 0.18823529411764706, 0.3803921568627451, 1.0), (0.12941176470588237, 0.4, 0.6745098039215687, 1.0), (0.2627450980392157, 0.5764705882352941, 0.7647058823529411, 1.0), (0.5725490196078431, 0.7725490196078432, 0.8705882352941177, 1.0), (0.8196078431372549, 0.8980392156862745, 0.9411764705882353, 1.0), (0.9686274509803922, 0.9686274509803922, 0.9686274509803922, 1.0), (0.9921568627450981, 0.8588235294117647, 0.7803921568627451, 1.0), (0.9568627450980393, 0.6470588235294118, 0.5098039215686274, 1.0), (0.8392156862745098, 0.3764705882352941, 0.30196078431372547, 1.0), (0.6980392156862745, 0.09411764705882353, 0.16862745098039217, 1.0), (0.403921568627451, 0.0, 0.12156862745098039, 1.0)]