create an asymmetric colormap

被刻印的时光 ゝ 提交于 2019-12-07 06:13:44

问题


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

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:


回答1:


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

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

Adding a third color:

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



来源:https://stackoverflow.com/questions/47846744/create-an-asymmetric-colormap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!