Using folium.colormap

A few examples of how to use folium.colormap in choropleths.


In [1]:
import pandas as pd
import json
import sys
sys.path.append('..')
import folium
print (folium.__file__)
print (folium.__version__)


../folium/__init__.py
0.2.0.dev

Let's load a GeoJSON file, and try to choropleth it.


In [2]:
geo_json_data = json.load(open('us-states.json'))
unemployment = pd.read_csv('./US_Unemployment_Oct2012.csv')
unemployment_dict = unemployment.set_index('State')['Unemployment']

Self-defined

You can build a choropleth in using a self-defined function. It has to output an hexadecimal color string of the form #RRGGBB or #RRGGBBAA.


In [3]:
def my_color_function(feature):
    """Maps low values to green and hugh values to red."""
    if unemployment_dict[feature['id']] > 6.5:
        return '#ff0000'
    else:
        return '#008000'

In [4]:
m = folium.Map([43,-100], tiles='cartodbpositron', zoom_start=4)

folium.GeoJson(
    geo_json_data,
    style_function=lambda feature: {
        'fillColor': my_color_function(feature),
        'color' : 'black',
        'weight' : 2,
        'dashArray' : '5, 5'
        }
    ).add_to(m)

m


Out[4]:

StepColormap

But to help you define you colormap, we've embedded StepColormap in folium.colormap.

You can simply define the colors you want, and the index (thresholds) that correspond.


In [5]:
import folium.colormap as cm

In [6]:
step = cm.StepColormap(['green','yellow','red'], vmin=3., vmax=10., index=[3,4,8,10], caption='step')
step


Out[6]:
3.010.0

In [7]:
m = folium.Map([43,-100], tiles='cartodbpositron', zoom_start=4)

folium.GeoJson(
    geo_json_data,
    style_function=lambda feature: {
        'fillColor': step(unemployment_dict[feature['id']]),
        'color' : 'black',
        'weight' : 2,
        'dashArray' : '5, 5'
        }
    ).add_to(m)

m


Out[7]:

If you specify no index, colors will be set uniformely.


In [8]:
cm.StepColormap(['r','y','g','c','b','m'])


Out[8]:
0.01.0

LinearColormap

But sometimes, you would prefer to have a continuous set of colors. This can be done by LinearColormap.


In [9]:
linear = cm.LinearColormap(['green','yellow','red'], vmin=3., vmax=10.)
linear


Out[9]:
3.010.0

In [10]:
m = folium.Map([43,-100], tiles='cartodbpositron', zoom_start=4)

folium.GeoJson(
    geo_json_data,
    style_function=lambda feature: {
        'fillColor': linear(unemployment_dict[feature['id']]),
        'color' : 'black',
        'weight' : 2,
        'dashArray' : '5, 5'
        }
    ).add_to(m)

m


Out[10]:

Again, you can set the index if you want something irregular.


In [11]:
cm.LinearColormap(['red','orange', 'yellow','green'], index=[0,0.1,0.9,1.])


Out[11]:
0.01.0

If you want to transform a linear map into a step one, you can use the method to_step.


In [12]:
linear.to_step(6)


Out[12]:
3.010.0

You can also use more sophisticated rules to create the thresholds.


In [13]:
linear.to_step(6,
               data=[30.6, 50, 51, 52, 53, 54, 55, 60, 70, 100],
               method='quantiles',
               round_method='int')


Out[13]:
31100

And the opposite is also possible with to_linear.


In [14]:
step.to_linear()


Out[14]:
3.010.0

Build-in

For convenience, we provide a (small) set of built-in linear colormaps, in folium.colormap.linear.


In [15]:
cm.linear.OrRd


Out[15]:
0.01.0

You can also use them to generate regular StepColormap.


In [16]:
cm.linear.PuBu.to_step(12)


Out[16]:
0.01.0

Of course, you may need to scale the colormaps to your bounds. This is doable with .scale.


In [17]:
cm.linear.YlGn.scale(3,12)


Out[17]:
312

In [18]:
cm.linear.RdGy.to_step(10).scale(5,100)


Out[18]:
5100

At last, if you want to check them all, simply ask for linear in the notebook.


In [19]:
cm.linear


Out[19]:
YlOrBr0.01.0
Paired0.01.0
PuOr0.01.0
Pastel20.01.0
Pastel10.01.0
YlGn0.01.0
PRGn0.01.0
GnBu0.01.0
YlGnBu0.01.0
RdBu0.01.0
RdGy0.01.0
BuPu0.01.0
RdYlGn0.01.0
Accent0.01.0
RdYlBu0.01.0
Spectral0.01.0
PuRd0.01.0
BuGn0.01.0
RdPu0.01.0
Set30.01.0
PuBuGn0.01.0
Set20.01.0
Set10.01.0
BrBg0.01.0
PuBu0.01.0
Dark20.01.0
OrRd0.01.0
PiYG0.01.0
YlOrRd0.01.0

Draw a ColorMap on a map

By the way, a ColorMap is also a Folium Element that you can draw on a map.


In [20]:
m = folium.Map(tiles='cartodbpositron')

colormap = cm.linear.Set1.scale(0,35).to_step(10)
colormap.caption = 'A colormap caption'
m.add_children(colormap)

m


Out[20]:

Conclusion

That's all folks !

Hope it'll be useful to you. Don't hesitate to provide a feedback on what can be improved, which method do you prefer, etc.