import os
import folium
print(folium.__version__)
0.8.3+52.g2758dc7.dirty
folium.colormap
¶A few examples of how to use folium.colormap
in choropleths.
Let's load a GeoJSON file, and try to choropleth it.
import json
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']
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
.
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'
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.save(os.path.join('results', 'Colormaps_0.html'))
m
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.
import branca.colormap as cm
step = cm.StepColormap(
['green', 'yellow', 'red'],
vmin=3, vmax=10,
index=[3, 4, 8, 10],
caption='step'
)
step
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.save(os.path.join('results', 'Colormaps_1.html'))
m
If you specify no index, colors will be set uniformely.
cm.StepColormap(['r', 'y', 'g', 'c', 'b', 'm'])
But sometimes, you would prefer to have a continuous set of colors. This can be done by LinearColormap
.
linear = cm.LinearColormap(
['green', 'yellow', 'red'],
vmin=3, vmax=10
)
linear
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.save(os.path.join('results', 'Colormaps_2.html'))
m
Again, you can set the index
if you want something irregular.
cm.LinearColormap(
['red', 'orange', 'yellow', 'green'],
index=[0, 0.1, 0.9, 1.0]
)
If you want to transform a linear map into a step one, you can use the method to_step
.
linear.to_step(6)
You can also use more sophisticated rules to create the thresholds.
linear.to_step(
n=6,
data=[30.6, 50, 51, 52, 53, 54, 55, 60, 70, 100],
method='quantiles',
round_method='int'
)
And the opposite is also possible with to_linear
.
step.to_linear()
For convenience, we provide a (small) set of built-in linear colormaps, in folium.colormap.linear
.
cm.linear.OrRd_09
You can also use them to generate regular StepColormap
.
cm.linear.PuBuGn_09.to_step(12)
Of course, you may need to scale the colormaps to your bounds. This is doable with .scale
.
cm.linear.YlGnBu_09.scale(3, 12)
cm.linear.RdGy_11.to_step(10).scale(5, 100)
At last, if you want to check them all, simply ask for linear
in the notebook.
cm.linear
viridis | |
Pastel1_03 | |
Pastel1_05 | |
Pastel1_04 | |
Pastel1_07 | |
YlOrRd_04 | |
Pastel1_09 | |
Pastel1_08 | |
Spectral_07 | |
RdYlBu_05 | |
PuBuGn_03 | |
Set1_08 | |
PuBuGn_05 | |
PuBuGn_04 | |
PuBuGn_07 | |
PuBuGn_06 | |
PuBuGn_09 | |
PuBuGn_08 | |
YlOrBr_04 | |
YlOrBr_05 | |
Set1_07 | |
YlOrBr_03 | |
Set1_05 | |
YlOrRd_03 | |
PuOr_06 | |
PuOr_07 | |
PuOr_04 | |
PuOr_05 | |
PuOr_03 | |
Purples_09 | |
Set2_06 | |
RdYlBu_11 | |
PuOr_08 | |
PuOr_09 | |
Paired_03 | |
RdBu_03 | |
RdYlBu_10 | |
Paired_07 | |
Paired_06 | |
Paired_05 | |
Paired_04 | |
Paired_09 | |
Paired_08 | |
RdGy_03 | |
PiYG_04 | |
Accent_03 | |
BuGn_08 | |
BuGn_09 | |
BuGn_04 | |
BuGn_05 | |
BuGn_06 | |
BuGn_07 | |
BuGn_03 | |
YlGnBu_07 | |
YlGnBu_06 | |
YlGnBu_05 | |
YlGnBu_04 | |
YlGnBu_03 | |
RdBu_06 | |
RdBu_05 | |
RdBu_04 | |
Accent_08 | |
RdBu_09 | |
RdBu_08 | |
Set2_04 | |
YlGnBu_09 | |
YlGnBu_08 | |
Blues_08 | |
Blues_09 | |
RdPu_09 | |
RdPu_08 | |
Set3_07 | |
Set3_06 | |
RdPu_05 | |
RdPu_04 | |
RdPu_07 | |
RdPu_06 | |
Blues_06 | |
Blues_07 | |
RdPu_03 | |
Blues_05 | |
Paired_10 | |
Paired_11 | |
Paired_12 | |
PuBu_06 | |
PuBu_07 | |
PuBu_04 | |
PuBu_05 | |
PuRd_05 | |
PuBu_03 | |
PuRd_07 | |
PuRd_06 | |
PuRd_09 | |
PuRd_08 | |
Set2_07 | |
PuBu_08 | |
PuBu_09 | |
RdBu_10 | |
RdBu_11 | |
Accent_06 | |
Set3_03 | |
Set3_05 | |
Set3_12 | |
Set3_10 | |
Set3_04 | |
RdGy_11 | |
RdGy_10 | |
Set1_03 | |
Set1_09 | |
Set3_09 | |
BuPu_08 | |
BuPu_09 | |
RdYlGn_11 | |
Blues_03 | |
Set2_05 | |
BuPu_03 | |
BuPu_06 | |
BuPu_07 | |
BuPu_04 | |
BuPu_05 | |
Accent_04 | |
YlOrRd_05 | |
YlOrBr_08 | |
Oranges_08 | |
Oranges_09 | |
Oranges_06 | |
Oranges_07 | |
Oranges_04 | |
YlOrBr_09 | |
Oranges_03 | |
YlOrBr_06 | |
Dark2_06 | |
Blues_04 | |
YlOrBr_07 | |
RdYlGn_05 | |
Set3_08 | |
YlOrRd_06 | |
Dark2_03 | |
Accent_05 | |
RdYlGn_08 | |
RdYlGn_09 | |
PuOr_11 | |
YlOrRd_07 | |
Spectral_11 | |
RdGy_08 | |
RdGy_09 | |
RdGy_06 | |
RdGy_07 | |
RdGy_04 | |
RdGy_05 | |
RdYlGn_04 | |
PiYG_09 | |
RdYlGn_06 | |
RdYlGn_07 | |
Spectral_04 | |
Spectral_05 | |
Spectral_06 | |
PiYG_08 | |
Set2_03 | |
Spectral_03 | |
Reds_08 | |
Set1_04 | |
Spectral_08 | |
Spectral_09 | |
Set2_08 | |
Reds_09 | |
Greys_07 | |
Greys_06 | |
Greys_05 | |
Greys_04 | |
Greys_03 | |
PuOr_10 | |
Accent_07 | |
Reds_06 | |
Greys_09 | |
Greys_08 | |
Reds_07 | |
RdYlBu_08 | |
RdYlBu_09 | |
BrBG_09 | |
BrBG_08 | |
BrBG_07 | |
BrBG_06 | |
BrBG_05 | |
BrBG_04 | |
BrBG_03 | |
PiYG_06 | |
Reds_03 | |
Set3_11 | |
Set1_06 | |
PuRd_03 | |
PiYG_07 | |
RdBu_07 | |
Pastel1_06 | |
Spectral_10 | |
PuRd_04 | |
OrRd_03 | |
PiYG_03 | |
Oranges_05 | |
OrRd_07 | |
OrRd_06 | |
OrRd_05 | |
OrRd_04 | |
Reds_04 | |
Reds_05 | |
OrRd_09 | |
OrRd_08 | |
BrBG_10 | |
BrBG_11 | |
PiYG_05 | |
YlOrRd_08 | |
GnBu_04 | |
GnBu_05 | |
GnBu_06 | |
GnBu_07 | |
Purples_08 | |
GnBu_03 | |
Purples_06 | |
Purples_07 | |
Purples_04 | |
Purples_05 | |
GnBu_08 | |
GnBu_09 | |
YlOrRd_09 | |
Purples_03 | |
RdYlBu_04 | |
PRGn_09 | |
PRGn_08 | |
PRGn_07 | |
PRGn_06 | |
PRGn_05 | |
PRGn_04 | |
PRGn_03 | |
RdYlBu_06 | |
RdYlGn_10 | |
YlGn_08 | |
YlGn_09 | |
RdYlBu_07 | |
PiYG_10 | |
PiYG_11 | |
YlGn_03 | |
YlGn_04 | |
YlGn_05 | |
YlGn_06 | |
YlGn_07 | |
Dark2_05 | |
Dark2_04 | |
Dark2_07 | |
Pastel2_03 | |
Pastel2_04 | |
Pastel2_05 | |
Pastel2_06 | |
Pastel2_07 | |
Pastel2_08 | |
RdYlBu_03 | |
Dark2_08 | |
RdYlGn_03 | |
PRGn_11 | |
Greens_08 | |
Greens_09 | |
Greens_06 | |
Greens_07 | |
Greens_04 | |
Greens_05 | |
PRGn_10 | |
Greens_03 |
ColorMap
on a map¶By the way, a ColorMap is also a Folium Element
that you can draw on a map.
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.save(os.path.join('results', 'Colormaps_3.html'))
m