Skip to content

Commit 896dd3d

Browse files
committed
deploy: 5c46067
1 parent 67096aa commit 896dd3d

File tree

274 files changed

+159394
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

274 files changed

+159394
-0
lines changed

v0.20.0/.buildinfo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Sphinx build info version 1
2+
# This file records the configuration used when building these files. When it is not found, a full rebuild will be done.
3+
config: 1e7b84d6a2ce813ffa9ff2abc58ab04b
4+
tags: 645f666f9bcd5a90fca523b33c5a78b7

v0.20.0/_images/folium_logo.png

1.62 KB
Loading
36.6 KB
Loading
60 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Advanced guide
2+
==============
3+
4+
.. toctree::
5+
:maxdepth: 1
6+
7+
advanced_guide/flask
8+
advanced_guide/choropleth with Jenks natural breaks optimization
9+
advanced_guide/subplots
10+
advanced_guide/colormaps
11+
advanced_guide/world_copy
12+
advanced_guide/custom_panes
13+
advanced_guide/geodedetic_image_overlay
14+
advanced_guide/custom_tiles
15+
advanced_guide/piechart_icons
16+
advanced_guide/polygons_from_list_of_points
17+
advanced_guide/customize_javascript_and_css
18+
advanced_guide/override_leaflet_class_methods
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Integrating Jenks Natural Break Optimization with choropleth
2+
3+
Choropleths provide an easy way to visually see data distributions across geography. By default, folium uses the breaks created by numpy.histogram (np.histogram), which generally creates an evenly spaced quantiles.
4+
5+
This works well enough for evenly distributed data, but for unevenly distributed data, these even quantiles can obscure more than they show. To demonstrate this, I have created maps showing the labor force of each US state.
6+
7+
The data was taken from the county-level data and aggregated. Since our geographic data does not have areas representing Puerto Rico or the United States as a whole, I removed those entries while keeping Washington, D.C. in our data set. Already, looking at the first five states alphabetically, we can see that Alaska (AK) has a work force roughly 2% the size of California (CA).
8+
9+
```{code-cell} ipython3
10+
import folium
11+
import numpy as np
12+
import pandas as pd
13+
import json
14+
import requests
15+
```
16+
17+
```{code-cell} ipython3
18+
geo_json_data = requests.get(
19+
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json"
20+
).json()
21+
22+
clf = 'Civilian_labor_force_2011'
23+
labor_force = pd.read_csv(
24+
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_labor_force_2011.csv"
25+
)
26+
27+
labor_force.head()
28+
```
29+
30+
Using default breaks, most states are represented as being part of the bottom quantile. This distribution is similar to what we might expect if US states follow a Power Law or a Zipf distribution.
31+
32+
```{code-cell} ipython3
33+
m = folium.Map(location=[38, -96], zoom_start=4)
34+
35+
folium.Choropleth(
36+
geo_data=geo_json_data,
37+
data=labor_force,
38+
columns=['State', clf],
39+
key_on='id',
40+
fill_color='RdBu',
41+
).add_to(m)
42+
43+
m
44+
```
45+
46+
However, when using Jenks natural Breaks Optimization, we now see more granular detail at the bottom of the distribution, where most of our states are located. The upper western states (Idaho, Montana, Wyoming and the Dakotas) are distinguished from their Midwestern and Mountain West neighbors to the south. Gradations in the deep south between Mississippi and Alabama provide more visual information than in the previous map. Overall, this is a richer representation of the data distribution.
47+
48+
One notable drawback of this representation is the legend. Because the lower bins are smaller, the numerical values overlap, making them unreadable.
49+
50+
```{code-cell} ipython3
51+
m = folium.Map(location=[38, -96], zoom_start=4)
52+
53+
choropleth = folium.Choropleth(
54+
geo_data=geo_json_data,
55+
data=labor_force,
56+
columns=['State', clf],
57+
key_on='id',
58+
fill_color='RdBu',
59+
use_jenks=True,
60+
)
61+
choropleth.add_to(m)
62+
63+
choropleth.color_scale.width = 800
64+
65+
m
66+
```
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
```{code-cell} ipython3
2+
---
3+
nbsphinx: hidden
4+
---
5+
import folium
6+
```
7+
8+
# Using colormaps
9+
10+
A few examples of how to use `folium.colormap` in choropleths.
11+
12+
Let's load a GeoJSON file, and try to choropleth it.
13+
14+
```{code-cell} ipython3
15+
import pandas
16+
import requests
17+
18+
geo_json_data = requests.get(
19+
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json"
20+
).json()
21+
unemployment = pandas.read_csv(
22+
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_unemployment_oct_2012.csv"
23+
)
24+
25+
unemployment_dict = unemployment.set_index("State")["Unemployment"]
26+
```
27+
28+
## Self-defined
29+
30+
You can build a choropleth in using a self-defined function.
31+
It has to output an hexadecimal color string of the form `#RRGGBB` or `#RRGGBBAA`.
32+
33+
```{code-cell} ipython3
34+
def my_color_function(feature):
35+
"""Maps low values to green and high values to red."""
36+
if unemployment_dict[feature["id"]] > 6.5:
37+
return "#ff0000"
38+
else:
39+
return "#008000"
40+
```
41+
42+
```{code-cell} ipython3
43+
m = folium.Map([43, -100], tiles="cartodbpositron", zoom_start=4)
44+
45+
folium.GeoJson(
46+
geo_json_data,
47+
style_function=lambda feature: {
48+
"fillColor": my_color_function(feature),
49+
"color": "black",
50+
"weight": 2,
51+
"dashArray": "5, 5",
52+
},
53+
).add_to(m)
54+
55+
m
56+
```
57+
58+
## StepColormap
59+
60+
But to help you define your colormap, we've embedded `StepColormap` in `folium.colormap`.
61+
62+
You can simply define the colors you want, and the `index` (*thresholds*) that correspond.
63+
64+
```{code-cell} ipython3
65+
import branca.colormap as cm
66+
67+
step = cm.StepColormap(
68+
["green", "yellow", "red"], vmin=3, vmax=10, index=[3, 4, 8, 10], caption="step"
69+
)
70+
71+
step
72+
```
73+
74+
```{code-cell} ipython3
75+
m = folium.Map([43, -100], tiles="cartodbpositron", zoom_start=4)
76+
77+
folium.GeoJson(
78+
geo_json_data,
79+
style_function=lambda feature: {
80+
"fillColor": step(unemployment_dict[feature["id"]]),
81+
"color": "black",
82+
"weight": 2,
83+
"dashArray": "5, 5",
84+
},
85+
).add_to(m)
86+
87+
m
88+
```
89+
90+
If you specify no index, colors will be set uniformly.
91+
92+
```{code-cell} ipython3
93+
cm.StepColormap(["r", "y", "g", "c", "b", "m"])
94+
```
95+
96+
## LinearColormap
97+
98+
But sometimes, you would prefer to have a *continuous* set of colors. This can be done by `LinearColormap`.
99+
100+
```{code-cell} ipython3
101+
linear = cm.LinearColormap(["green", "yellow", "red"], vmin=3, vmax=10)
102+
103+
linear
104+
```
105+
106+
```{code-cell} ipython3
107+
m = folium.Map([43, -100], tiles="cartodbpositron", zoom_start=4)
108+
109+
folium.GeoJson(
110+
geo_json_data,
111+
style_function=lambda feature: {
112+
"fillColor": linear(unemployment_dict[feature["id"]]),
113+
"color": "black",
114+
"weight": 2,
115+
"dashArray": "5, 5",
116+
},
117+
).add_to(m)
118+
119+
m
120+
```
121+
122+
Again, you can set the `index` if you want something irregular.
123+
124+
```{code-cell} ipython3
125+
cm.LinearColormap(["red", "orange", "yellow", "green"], index=[0, 0.1, 0.9, 1.0])
126+
```
127+
128+
If you want to transform a linear map into a *step* one, you can use the method `to_step`.
129+
130+
```{code-cell} ipython3
131+
linear.to_step(6)
132+
```
133+
134+
You can also use more sophisticated rules to create the thresholds.
135+
136+
```{code-cell} ipython3
137+
linear.to_step(
138+
n=6,
139+
data=[30.6, 50, 51, 52, 53, 54, 55, 60, 70, 100],
140+
method="quantiles",
141+
round_method="int",
142+
)
143+
```
144+
145+
And the opposite is also possible with `to_linear`.
146+
147+
```{code-cell} ipython3
148+
step.to_linear()
149+
```
150+
151+
## Built-in
152+
153+
For convenience, we provide a (small) set of built-in linear colormaps, in `folium.colormap.linear`.
154+
155+
```{code-cell} ipython3
156+
cm.linear.OrRd_09
157+
```
158+
159+
You can also use them to generate regular `StepColormap`.
160+
161+
```{code-cell} ipython3
162+
cm.linear.PuBuGn_09.to_step(12)
163+
```
164+
165+
Of course, you may need to scale the colormaps to your bounds. This is doable with `.scale`.
166+
167+
```{code-cell} ipython3
168+
cm.linear.YlGnBu_09.scale(3, 12)
169+
```
170+
171+
```{code-cell} ipython3
172+
cm.linear.RdGy_11.to_step(10).scale(5, 100)
173+
```
174+
175+
At last, if you want to check them all, simply ask for `linear` in the notebook.
176+
177+
```{code-cell} ipython3
178+
cm.linear
179+
```
180+
181+
## Draw a `ColorMap` on a map
182+
183+
By the way, a ColorMap is also a Folium `Element` that you can draw on a map.
184+
185+
```{code-cell} ipython3
186+
m = folium.Map(tiles="cartodbpositron")
187+
188+
colormap = cm.linear.Set1_09.scale(0, 35).to_step(10)
189+
colormap.caption = "A colormap caption"
190+
m.add_child(colormap)
191+
192+
m
193+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
```{code-cell} ipython3
2+
---
3+
nbsphinx: hidden
4+
---
5+
import folium
6+
```
7+
8+
# Panes and CustomPane
9+
10+
Panes are used to control the ordering of layers on the map. You can customise
11+
them using the `CustomPane` class.
12+
13+
For more info on the panes Leaflet has, see https://leafletjs.com/reference.html#map-pane.
14+
15+
First we'll load geojson data to use in the examples:
16+
17+
```{code-cell} ipython3
18+
import requests
19+
20+
geo_json_data = requests.get(
21+
"https://raw.githubusercontent.com/python-visualization/folium-example-data/main/us_states.json"
22+
).json()
23+
24+
style_function = lambda x: {"fillOpacity": 0.8}
25+
```
26+
27+
## Map without custom pane
28+
29+
We'll make an example to show how the GeoJson we add hides any labels
30+
underneath.
31+
32+
```{code-cell} ipython3
33+
m = folium.Map([43, -100], zoom_start=4)
34+
35+
folium.GeoJson(geo_json_data, style_function=style_function).add_to(m)
36+
37+
m
38+
```
39+
40+
## Map with custom pane
41+
42+
Now we'll create a custom pane and add a tile layer that contains only labels.
43+
The labels will show on top off the geojson.
44+
45+
```{code-cell} ipython3
46+
m = folium.Map([43, -100], zoom_start=4, tiles="cartodbpositronnolabels")
47+
48+
folium.GeoJson(geo_json_data, style_function=style_function).add_to(m)
49+
50+
folium.map.CustomPane("labels").add_to(m)
51+
52+
# Final layer associated to custom pane via the appropriate kwarg
53+
folium.TileLayer("cartodbpositrononlylabels", pane="labels").add_to(m)
54+
55+
m
56+
```

0 commit comments

Comments
 (0)