Using colormaps#

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

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

[2]:
import pandas
import requests

geo_json_data = requests.get(
    "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json"
).json()
unemployment = pandas.read_csv(
    "https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_unemployment_oct_2012.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.

[3]:
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"
[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
[4]:
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.

[5]:
import branca.colormap as cm

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

step
[5]:
34.25.36.57.78.810step
[6]:
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
[6]:
Make this Notebook Trusted to load map: File -> Trust Notebook

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

[7]:
cm.StepColormap(["r", "y", "g", "c", "b", "m"])
[7]:
0.00.20.30.50.70.81.0

LinearColormap#

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

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

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

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

[10]:
cm.LinearColormap(["red", "orange", "yellow", "green"], index=[0, 0.1, 0.9, 1.0])
[10]:
0.00.20.30.50.70.81.0

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

[11]:
linear.to_step(6)
[11]:
3.04.25.36.57.78.810.0

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

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

And the opposite is also possible with to_linear.

[13]:
step.to_linear()
[13]:
34.25.36.57.78.810step

Built-in#

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

[14]:
cm.linear.OrRd_09
[14]:
0.00.20.30.50.70.81.0

You can also use them to generate regular StepColormap.

[15]:
cm.linear.PuBuGn_09.to_step(12)
[15]:
0.00.20.30.50.70.81.0

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

[16]:
cm.linear.YlGnBu_09.scale(3, 12)
[16]:
34.56.07.59.010.512
[17]:
cm.linear.RdGy_11.to_step(10).scale(5, 100)
[17]:
520.836.752.568.384.2100

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

[18]:
cm.linear
[18]:
viridis0.00.20.30.50.70.81.0
plasma0.00.20.30.50.70.81.0
inferno0.00.20.30.50.70.81.0
magma0.00.20.30.50.70.81.0
Pastel1_030.00.20.30.50.70.81.0
Pastel1_050.00.20.30.50.70.81.0
Pastel1_040.00.20.30.50.70.81.0
Pastel1_070.00.20.30.50.70.81.0
YlOrRd_040.00.20.30.50.70.81.0
Pastel1_090.00.20.30.50.70.81.0
Pastel1_080.00.20.30.50.70.81.0
Spectral_070.00.20.30.50.70.81.0
RdYlBu_050.00.20.30.50.70.81.0
PuBuGn_030.00.20.30.50.70.81.0
Set1_080.00.20.30.50.70.81.0
PuBuGn_050.00.20.30.50.70.81.0
PuBuGn_040.00.20.30.50.70.81.0
PuBuGn_070.00.20.30.50.70.81.0
PuBuGn_060.00.20.30.50.70.81.0
PuBuGn_090.00.20.30.50.70.81.0
PuBuGn_080.00.20.30.50.70.81.0
YlOrBr_040.00.20.30.50.70.81.0
YlOrBr_050.00.20.30.50.70.81.0
Set1_070.00.20.30.50.70.81.0
YlOrBr_030.00.20.30.50.70.81.0
Set1_050.00.20.30.50.70.81.0
YlOrRd_030.00.20.30.50.70.81.0
PuOr_060.00.20.30.50.70.81.0
PuOr_070.00.20.30.50.70.81.0
PuOr_040.00.20.30.50.70.81.0
PuOr_050.00.20.30.50.70.81.0
PuOr_030.00.20.30.50.70.81.0
Purples_090.00.20.30.50.70.81.0
Set2_060.00.20.30.50.70.81.0
RdYlBu_110.00.20.30.50.70.81.0
PuOr_080.00.20.30.50.70.81.0
PuOr_090.00.20.30.50.70.81.0
Paired_030.00.20.30.50.70.81.0
RdBu_030.00.20.30.50.70.81.0
RdYlBu_100.00.20.30.50.70.81.0
Paired_070.00.20.30.50.70.81.0
Paired_060.00.20.30.50.70.81.0
Paired_050.00.20.30.50.70.81.0
Paired_040.00.20.30.50.70.81.0
Paired_090.00.20.30.50.70.81.0
Paired_080.00.20.30.50.70.81.0
RdGy_030.00.20.30.50.70.81.0
PiYG_040.00.20.30.50.70.81.0
Accent_030.00.20.30.50.70.81.0
BuGn_080.00.20.30.50.70.81.0
BuGn_090.00.20.30.50.70.81.0
BuGn_040.00.20.30.50.70.81.0
BuGn_050.00.20.30.50.70.81.0
BuGn_060.00.20.30.50.70.81.0
BuGn_070.00.20.30.50.70.81.0
BuGn_030.00.20.30.50.70.81.0
YlGnBu_070.00.20.30.50.70.81.0
YlGnBu_060.00.20.30.50.70.81.0
YlGnBu_050.00.20.30.50.70.81.0
YlGnBu_040.00.20.30.50.70.81.0
YlGnBu_030.00.20.30.50.70.81.0
RdBu_060.00.20.30.50.70.81.0
RdBu_050.00.20.30.50.70.81.0
RdBu_040.00.20.30.50.70.81.0
Accent_080.00.20.30.50.70.81.0
RdBu_090.00.20.30.50.70.81.0
RdBu_080.00.20.30.50.70.81.0
Set2_040.00.20.30.50.70.81.0
YlGnBu_090.00.20.30.50.70.81.0
YlGnBu_080.00.20.30.50.70.81.0
Blues_080.00.20.30.50.70.81.0
Blues_090.00.20.30.50.70.81.0
RdPu_090.00.20.30.50.70.81.0
RdPu_080.00.20.30.50.70.81.0
Set3_070.00.20.30.50.70.81.0
Set3_060.00.20.30.50.70.81.0
RdPu_050.00.20.30.50.70.81.0
RdPu_040.00.20.30.50.70.81.0
RdPu_070.00.20.30.50.70.81.0
RdPu_060.00.20.30.50.70.81.0
Blues_060.00.20.30.50.70.81.0
Blues_070.00.20.30.50.70.81.0
RdPu_030.00.20.30.50.70.81.0
Blues_050.00.20.30.50.70.81.0
Paired_100.00.20.30.50.70.81.0
Paired_110.00.20.30.50.70.81.0
Paired_120.00.20.30.50.70.81.0
PuBu_060.00.20.30.50.70.81.0
PuBu_070.00.20.30.50.70.81.0
PuBu_040.00.20.30.50.70.81.0
PuBu_050.00.20.30.50.70.81.0
PuRd_050.00.20.30.50.70.81.0
PuBu_030.00.20.30.50.70.81.0
PuRd_070.00.20.30.50.70.81.0
PuRd_060.00.20.30.50.70.81.0
PuRd_090.00.20.30.50.70.81.0
PuRd_080.00.20.30.50.70.81.0
Set2_070.00.20.30.50.70.81.0
PuBu_080.00.20.30.50.70.81.0
PuBu_090.00.20.30.50.70.81.0
RdBu_100.00.20.30.50.70.81.0
RdBu_110.00.20.30.50.70.81.0
Accent_060.00.20.30.50.70.81.0
Set3_030.00.20.30.50.70.81.0
Set3_050.00.20.30.50.70.81.0
Set3_120.00.20.30.50.70.81.0
Set3_100.00.20.30.50.70.81.0
Set3_040.00.20.30.50.70.81.0
RdGy_110.00.20.30.50.70.81.0
RdGy_100.00.20.30.50.70.81.0
Set1_030.00.20.30.50.70.81.0
Set1_090.00.20.30.50.70.81.0
Set3_090.00.20.30.50.70.81.0
BuPu_080.00.20.30.50.70.81.0
BuPu_090.00.20.30.50.70.81.0
RdYlGn_110.00.20.30.50.70.81.0
Blues_030.00.20.30.50.70.81.0
Set2_050.00.20.30.50.70.81.0
BuPu_030.00.20.30.50.70.81.0
BuPu_060.00.20.30.50.70.81.0
BuPu_070.00.20.30.50.70.81.0
BuPu_040.00.20.30.50.70.81.0
BuPu_050.00.20.30.50.70.81.0
Accent_040.00.20.30.50.70.81.0
YlOrRd_050.00.20.30.50.70.81.0
YlOrBr_080.00.20.30.50.70.81.0
Oranges_080.00.20.30.50.70.81.0
Oranges_090.00.20.30.50.70.81.0
Oranges_060.00.20.30.50.70.81.0
Oranges_070.00.20.30.50.70.81.0
Oranges_040.00.20.30.50.70.81.0
YlOrBr_090.00.20.30.50.70.81.0
Oranges_030.00.20.30.50.70.81.0
YlOrBr_060.00.20.30.50.70.81.0
Dark2_060.00.20.30.50.70.81.0
Blues_040.00.20.30.50.70.81.0
YlOrBr_070.00.20.30.50.70.81.0
RdYlGn_050.00.20.30.50.70.81.0
Set3_080.00.20.30.50.70.81.0
YlOrRd_060.00.20.30.50.70.81.0
Dark2_030.00.20.30.50.70.81.0
Accent_050.00.20.30.50.70.81.0
RdYlGn_080.00.20.30.50.70.81.0
RdYlGn_090.00.20.30.50.70.81.0
PuOr_110.00.20.30.50.70.81.0
YlOrRd_070.00.20.30.50.70.81.0
Spectral_110.00.20.30.50.70.81.0
RdGy_080.00.20.30.50.70.81.0
RdGy_090.00.20.30.50.70.81.0
RdGy_060.00.20.30.50.70.81.0
RdGy_070.00.20.30.50.70.81.0
RdGy_040.00.20.30.50.70.81.0
RdGy_050.00.20.30.50.70.81.0
RdYlGn_040.00.20.30.50.70.81.0
PiYG_090.00.20.30.50.70.81.0
RdYlGn_060.00.20.30.50.70.81.0
RdYlGn_070.00.20.30.50.70.81.0
Spectral_040.00.20.30.50.70.81.0
Spectral_050.00.20.30.50.70.81.0
Spectral_060.00.20.30.50.70.81.0
PiYG_080.00.20.30.50.70.81.0
Set2_030.00.20.30.50.70.81.0
Spectral_030.00.20.30.50.70.81.0
Reds_080.00.20.30.50.70.81.0
Set1_040.00.20.30.50.70.81.0
Spectral_080.00.20.30.50.70.81.0
Spectral_090.00.20.30.50.70.81.0
Set2_080.00.20.30.50.70.81.0
Reds_090.00.20.30.50.70.81.0
Greys_070.00.20.30.50.70.81.0
Greys_060.00.20.30.50.70.81.0
Greys_050.00.20.30.50.70.81.0
Greys_040.00.20.30.50.70.81.0
Greys_030.00.20.30.50.70.81.0
PuOr_100.00.20.30.50.70.81.0
Accent_070.00.20.30.50.70.81.0
Reds_060.00.20.30.50.70.81.0
Greys_090.00.20.30.50.70.81.0
Greys_080.00.20.30.50.70.81.0
Reds_070.00.20.30.50.70.81.0
RdYlBu_080.00.20.30.50.70.81.0
RdYlBu_090.00.20.30.50.70.81.0
BrBG_090.00.20.30.50.70.81.0
BrBG_080.00.20.30.50.70.81.0
BrBG_070.00.20.30.50.70.81.0
BrBG_060.00.20.30.50.70.81.0
BrBG_050.00.20.30.50.70.81.0
BrBG_040.00.20.30.50.70.81.0
BrBG_030.00.20.30.50.70.81.0
PiYG_060.00.20.30.50.70.81.0
Reds_030.00.20.30.50.70.81.0
Set3_110.00.20.30.50.70.81.0
Set1_060.00.20.30.50.70.81.0
PuRd_030.00.20.30.50.70.81.0
PiYG_070.00.20.30.50.70.81.0
RdBu_070.00.20.30.50.70.81.0
Pastel1_060.00.20.30.50.70.81.0
Spectral_100.00.20.30.50.70.81.0
PuRd_040.00.20.30.50.70.81.0
OrRd_030.00.20.30.50.70.81.0
PiYG_030.00.20.30.50.70.81.0
Oranges_050.00.20.30.50.70.81.0
OrRd_070.00.20.30.50.70.81.0
OrRd_060.00.20.30.50.70.81.0
OrRd_050.00.20.30.50.70.81.0
OrRd_040.00.20.30.50.70.81.0
Reds_040.00.20.30.50.70.81.0
Reds_050.00.20.30.50.70.81.0
OrRd_090.00.20.30.50.70.81.0
OrRd_080.00.20.30.50.70.81.0
BrBG_100.00.20.30.50.70.81.0
BrBG_110.00.20.30.50.70.81.0
PiYG_050.00.20.30.50.70.81.0
YlOrRd_080.00.20.30.50.70.81.0
GnBu_040.00.20.30.50.70.81.0
GnBu_050.00.20.30.50.70.81.0
GnBu_060.00.20.30.50.70.81.0
GnBu_070.00.20.30.50.70.81.0
Purples_080.00.20.30.50.70.81.0
GnBu_030.00.20.30.50.70.81.0
Purples_060.00.20.30.50.70.81.0
Purples_070.00.20.30.50.70.81.0
Purples_040.00.20.30.50.70.81.0
Purples_050.00.20.30.50.70.81.0
GnBu_080.00.20.30.50.70.81.0
GnBu_090.00.20.30.50.70.81.0
YlOrRd_090.00.20.30.50.70.81.0
Purples_030.00.20.30.50.70.81.0
RdYlBu_040.00.20.30.50.70.81.0
PRGn_090.00.20.30.50.70.81.0
PRGn_080.00.20.30.50.70.81.0
PRGn_070.00.20.30.50.70.81.0
PRGn_060.00.20.30.50.70.81.0
PRGn_050.00.20.30.50.70.81.0
PRGn_040.00.20.30.50.70.81.0
PRGn_030.00.20.30.50.70.81.0
RdYlBu_060.00.20.30.50.70.81.0
RdYlGn_100.00.20.30.50.70.81.0
YlGn_080.00.20.30.50.70.81.0
YlGn_090.00.20.30.50.70.81.0
RdYlBu_070.00.20.30.50.70.81.0
PiYG_100.00.20.30.50.70.81.0
PiYG_110.00.20.30.50.70.81.0
YlGn_030.00.20.30.50.70.81.0
YlGn_040.00.20.30.50.70.81.0
YlGn_050.00.20.30.50.70.81.0
YlGn_060.00.20.30.50.70.81.0
YlGn_070.00.20.30.50.70.81.0
Dark2_050.00.20.30.50.70.81.0
Dark2_040.00.20.30.50.70.81.0
Dark2_070.00.20.30.50.70.81.0
Pastel2_030.00.20.30.50.70.81.0
Pastel2_040.00.20.30.50.70.81.0
Pastel2_050.00.20.30.50.70.81.0
Pastel2_060.00.20.30.50.70.81.0
Pastel2_070.00.20.30.50.70.81.0
Pastel2_080.00.20.30.50.70.81.0
RdYlBu_030.00.20.30.50.70.81.0
Dark2_080.00.20.30.50.70.81.0
RdYlGn_030.00.20.30.50.70.81.0
PRGn_110.00.20.30.50.70.81.0
Greens_080.00.20.30.50.70.81.0
Greens_090.00.20.30.50.70.81.0
Greens_060.00.20.30.50.70.81.0
Greens_070.00.20.30.50.70.81.0
Greens_040.00.20.30.50.70.81.0
Greens_050.00.20.30.50.70.81.0
PRGn_100.00.20.30.50.70.81.0
Greens_030.00.20.30.50.70.81.0

Draw a ColorMap on a map#

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

[19]:
m = folium.Map(tiles="cartodbpositron")

colormap = cm.linear.Set1_09.scale(0, 35).to_step(10)
colormap.caption = "A colormap caption"
m.add_child(colormap)

m
[19]:
Make this Notebook Trusted to load map: File -> Trust Notebook