Using folium.colormap

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

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

In [1]:
import json

import folium
import pandas as pd
import requests


url = (
    "https://raw.githubusercontent.com/python-visualization/folium/master/examples/data"
)
us_states = f"{url}/us-states.json"
US_Unemployment_Oct2012 = f"{url}/US_Unemployment_Oct2012.csv"

geo_json_data = json.loads(requests.get(us_states).text)
unemployment = pd.read_csv(US_Unemployment_Oct2012)

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 [2]:
def my_color_function(feature):
    """Maps low values to green and high values to red."""
    if unemployment_dict[feature["id"]] > 6.5:
        return "#ff0000"
    else:
        return "#008000"
In [3]:
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[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook

StepColormap

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

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

In [4]:
import branca.colormap as cm

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

step
Out[4]:
310
In [5]:
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[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook

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

In [6]:
cm.StepColormap(["r", "y", "g", "c", "b", "m"])
Out[6]:
0.01.0

LinearColormap

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

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

linear
Out[7]:
310
In [8]:
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[8]:
Make this Notebook Trusted to load map: File -> Trust Notebook

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

In [9]:
cm.LinearColormap(["red", "orange", "yellow", "green"], index=[0, 0.1, 0.9, 1.0])
Out[9]:
0.01.0

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

In [10]:
linear.to_step(6)
Out[10]:
3.010.0

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

In [11]:
linear.to_step(
    n=6,
    data=[30.6, 50, 51, 52, 53, 54, 55, 60, 70, 100],
    method="quantiles",
    round_method="int",
)
Out[11]:
31100

And the opposite is also possible with to_linear.

In [12]:
step.to_linear()
Out[12]:
310

Build-in

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

In [13]:
cm.linear.OrRd_09
Out[13]:
0.01.0

You can also use them to generate regular StepColormap.

In [14]:
cm.linear.PuBuGn_09.to_step(12)
Out[14]:
0.01.0

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

In [15]:
cm.linear.YlGnBu_09.scale(3, 12)
Out[15]:
312
In [16]:
cm.linear.RdGy_11.to_step(10).scale(5, 100)
Out[16]:
5100

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

In [17]:
cm.linear
Out[17]:
viridis0.01.0
Pastel1_030.01.0
Pastel1_050.01.0
Pastel1_04