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]:
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]:
[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]:
If you specify no index, colors will be set uniformly.
[7]:
cm.StepColormap(["r", "y", "g", "c", "b", "m"])
[7]:
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]:
[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]:
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]:
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]:
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]:
And the opposite is also possible with to_linear
.
[13]:
step.to_linear()
[13]:
Built-in#
For convenience, we provide a (small) set of built-in linear colormaps, in folium.colormap.linear
.
[14]:
cm.linear.OrRd_09
[14]:
You can also use them to generate regular StepColormap
.
[15]:
cm.linear.PuBuGn_09.to_step(12)
[15]:
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]:
[17]:
cm.linear.RdGy_11.to_step(10).scale(5, 100)
[17]:
At last, if you want to check them all, simply ask for linear
in the notebook.
[18]:
cm.linear
[18]:
viridis | |
plasma | |
inferno | |
magma | |
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 |