Search code examples
pythonfolium

create an asymmetric colormap


I am creating a colormap to map colors in a folium choropleth map, using code from here:

from branca.colormap import linear

colormap = linear.RdBu.scale(
    df.MyValue.min(),
    df.MyValue.max())

colormap

my colormap

As you can see, the min and max values are skewed compared to the 0, and I'd like this to be reflected in the colormap, i.e. only values < 0 should be mapped to red, while all positive values should be mapped to blue.

Is there a way to have this asymmetry in the colormap? I haven't seen many examples on the web.

I am doing like:

colormap = colormap.to_step(index=[-200, 0, 1200])

but it's not smooth:

enter image description here


Solution

  • You can set the index if you want an asymmetrical Color Scale (in the documentation is called irregular). Example:

    import branca.colormap as cm
    
    colormap = cm.LinearColormap(colors=['red','blue'], index=[-200,0],vmin=-200,vmax=1200)
    colormap
    

    enter image description here

    The transition will be given by your index. For example, these are other transitions:

    colormap = cm.LinearColormap(colors=['red','blue'], index=[0,200],vmin=-200,vmax=1200)
    colormap
    

    enter image description here

    Adding a third color:

       colormap = cm.LinearColormap(colors=['red','lightblue','blue'], index=[-200,0,1200],vmin=-200,vmax=1200)
        colormap
    

    enter image description here