Skip to content

Commit b50d1b7

Browse files
committed
use new example data functions in docs
1 parent 4db3249 commit b50d1b7

14 files changed

Lines changed: 4232 additions & 152 deletions

docs/advanced_guide/choropleth with Jenks natural breaks optimization.md

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,10 @@ import requests
1515
```
1616

1717
```{code-cell} ipython3
18-
url = (
19-
"https://raw.githubusercontent.com/python-visualization/folium/main/examples/data"
20-
)
21-
us_states = f"{url}/us-states.json"
22-
23-
geo_json_data = json.loads(requests.get(us_states).text)
18+
geo_json_data = folium.example_data.us_states_geojson()
2419
25-
county_data = pd.read_csv(f"{url}/us_county_data.csv")
2620
clf = 'Civilian_labor_force_2011'
27-
labor_force = county_data[['State', clf]][
28-
(county_data[clf].str.strip()!='') & (~county_data['State'].isin(['PR', 'US']))
29-
]
30-
labor_force[clf] = labor_force[clf].astype(int)
31-
labor_force = labor_force.groupby('State').sum().reset_index()
21+
labor_force = folium.example_data.us_labor_force_pandas_dataframe()
3222
3323
labor_force.head()
3424
```

docs/advanced_guide/colormaps.md

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
1-
# Using `folium.colormap`
2-
3-
**A few examples of how to use `folium.colormap` in choropleths.**
4-
5-
Let's load a GeoJSON file, and try to choropleth it.
6-
71
```{code-cell} ipython3
8-
import json
9-
2+
---
3+
nbsphinx: hidden
4+
---
105
import folium
11-
import pandas as pd
12-
import requests
6+
```
137

8+
# Using colormaps
149

15-
url = (
16-
"https://raw.githubusercontent.com/python-visualization/folium/main/examples/data"
17-
)
18-
us_states = f"{url}/us-states.json"
19-
US_Unemployment_Oct2012 = f"{url}/US_Unemployment_Oct2012.csv"
10+
A few examples of how to use `folium.colormap` in choropleths.
2011

21-
geo_json_data = json.loads(requests.get(us_states).text)
22-
unemployment = pd.read_csv(US_Unemployment_Oct2012)
12+
Let's load a GeoJSON file, and try to choropleth it.
13+
14+
```{code-cell} ipython3
15+
geo_json_data = folium.example_data.us_states_geojson()
16+
unemployment = folium.example_data.us_unemployment_pandas_dataframe()
2317
2418
unemployment_dict = unemployment.set_index("State")["Unemployment"]
2519
```

docs/advanced_guide/custom_panes.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
```{code-cell} ipython3
2+
---
3+
nbsphinx: hidden
4+
---
5+
import folium
6+
```
7+
18
# Panes and CustomPane
29

310
Panes are used to control the ordering of layers on the map. You can customise
@@ -8,16 +15,7 @@ For more info on the panes Leaflet has, see https://leafletjs.com/reference.html
815
First we'll load geojson data to use in the examples:
916

1017
```{code-cell} ipython3
11-
import json
12-
13-
import folium
14-
import requests
15-
16-
url = (
17-
"https://raw.githubusercontent.com/python-visualization/folium/main/examples/data"
18-
)
19-
us_states = f"{url}/us-states.json"
20-
geo_json_data = json.loads(requests.get(us_states).text)
18+
geo_json_data = folium.example_data.us_states_geojson()
2119
2220
style_function = lambda x: {"fillOpacity": 0.8}
2321
```

docs/advanced_guide/custom_tiles.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
```{code-cell} ipython3
2+
---
3+
nbsphinx: hidden
4+
---
5+
import folium
6+
```
7+
18
# Custom tiles
29

310
## No tiles
411

512
```{code-cell} ipython3
6-
import os
7-
import json
8-
9-
with open("../data/us-states.json") as f:
10-
states = json.load(f)
13+
states = folium.example_data.us_states_geojson()
1114
1215
kw = {"location": [48, -102], "zoom_start": 3}
1316
```
1417

1518
```{code-cell} ipython3
16-
import folium
17-
from folium import GeoJson
18-
19-
2019
m = folium.Map(tiles=None, **kw)
2120
22-
GeoJson(states).add_to(m)
21+
folium.GeoJson(states).add_to(m)
2322
2423
m
2524
```
@@ -35,7 +34,7 @@ white_tile = branca.utilities.image_to_url([[1, 1], [1, 1]])
3534
# Create a map using this url for each tile.
3635
m = folium.Map(tiles=white_tile, attr="white tile", **kw)
3736
38-
GeoJson(states).add_to(m)
37+
folium.GeoJson(states).add_to(m)
3938
4039
m
4140
```
@@ -49,7 +48,7 @@ tiles = branca.utilities.image_to_url(images)
4948
5049
m = folium.Map(tiles=tiles, attr="Just because we can", **kw)
5150
52-
GeoJson(states).add_to(m)
51+
folium.GeoJson(states).add_to(m)
5352
5453
m
5554
```
@@ -61,7 +60,7 @@ tiles = branca.utilities.image_to_url(images)
6160
6261
m = folium.Map(tiles=tiles, attr="Just because we can", **kw)
6362
64-
GeoJson(states).add_to(m)
63+
folium.GeoJson(states).add_to(m)
6564
6665
m
6766
```

docs/advanced_guide/piechart_icons.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
```{code-cell} ipython3
2+
---
3+
nbsphinx: hidden
4+
---
5+
import folium
6+
```
7+
18
# Piechart icons
29

310
In this example we show how you can make mini-charts on several locations.
411
We'll make little piecharts showing the number of consonants and vowels in
512
a couple of languages. Those piecharts will be included as icons on the map.
613

714
```{code-cell} ipython3
8-
import os
9-
import ast
10-
11-
import pandas
12-
13-
data = pandas.read_csv(
14-
"../data/consonants_vowels.csv",
15-
# To ensure that tuples are read as tuples
16-
converters={"coordinates": ast.literal_eval},
17-
)
15+
data = folium.example_data.language_coordinates_and_stats_pandas_dataframe()
1816
1917
data.head()
2018
```
@@ -88,9 +86,6 @@ legend._template = branca.element.Template(legend_html)
8886
## Map
8987

9088
```{code-cell} ipython3
91-
import folium
92-
93-
9489
m = folium.Map(location=(0, 0), zoom_start=2)
9590
9691
for i, coord in enumerate(data.coordinates):
@@ -103,5 +98,6 @@ for i, coord in enumerate(data.coordinates):
10398
marker.add_child(popup)
10499
m.add_child(marker)
105100
m.get_root().add_child(legend)
101+
106102
m
107103
```

docs/getting_started.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ m
149149
GeoJSON/TopoJSON overlays
150150
-------------------------
151151

152-
Folium supports both GeoJSON and TopoJSON data in various formats. Here's an example where
153-
the data is retrieved from a url:
152+
Folium supports both GeoJSON and TopoJSON data in various formats, such as urls,
153+
file paths and dictionaries.
154154

155155
```{code-cell} ipython3
156156
m = folium.Map(tiles="cartodbpositron")
157157
158-
url = ("https://raw.githubusercontent.com/python-visualization/folium"
159-
"/main/examples/data/world-countries.json")
160-
folium.GeoJson(url, name="hello world").add_to(m)
158+
geojson_data = folium.example_data.world_countries_geojson()
159+
160+
folium.GeoJson(geojson_data, name="hello world").add_to(m)
161161
162162
folium.LayerControl().add_to(m)
163163
@@ -171,12 +171,8 @@ Choropleth maps
171171
Choropleth can be created by binding the data between Pandas DataFrames/Series and Geo/TopoJSON geometries.
172172

173173
```{code-cell} ipython3
174-
import pandas as pd
175-
176-
url = "https://raw.githubusercontent.com/python-visualization/folium/main/examples/data"
177-
state_geo = f"{url}/us-states.json"
178-
state_unemployment = f"{url}/US_Unemployment_Oct2012.csv"
179-
state_data = pd.read_csv(state_unemployment)
174+
state_geo = folium.example_data.us_states_geojson()
175+
state_data = folium.example_data.us_unemployment_pandas_dataframe()
180176
181177
m = folium.Map(location=[48, -102], zoom_start=3)
182178

docs/user_guide/geojson/choropleth.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
## Using `Choropleth`
2-
31
```{code-cell} ipython3
42
---
53
nbsphinx: hidden
64
---
75
import folium
86
```
97

8+
## Using `Choropleth`
9+
1010
Now if you want to get faster, you can use the `Choropleth` class. Have a look at it's docstring, it has several styling options.
1111

1212
Just like the `GeoJson` class you can provide it a filename, a dict, or a geopandas object.
1313

1414
```{code-cell} ipython3
15-
us_states = "../../data/us-states.json"
16-
1715
m = folium.Map([43, -100], zoom_start=4)
1816
17+
us_states = folium.example_data.us_states_geojson()
18+
1919
folium.Choropleth(
2020
geo_data=us_states,
2121
fill_opacity=0.3,
@@ -28,9 +28,7 @@ m
2828
Then, in playing with keyword arguments, you can get a choropleth in a few lines:
2929

3030
```{code-cell} ipython3
31-
import pandas as pd
32-
33-
state_data = pd.read_csv("../../data/US_Unemployment_Oct2012.csv")
31+
state_data = folium.example_data.us_unemployment_pandas_dataframe()
3432
3533
m = folium.Map([43, -100], zoom_start=4)
3634

docs/user_guide/geojson/geojson.md

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
```{code-cell} ipython3
2+
---
3+
nbsphinx: hidden
4+
---
5+
import folium
6+
```
7+
18
## Using `GeoJson`
29

310
### Loading data
411

512
Let us load a GeoJSON file representing the US states.
613

714
```{code-cell} ipython3
8-
import json
9-
10-
import requests
11-
12-
url = (
13-
"https://raw.githubusercontent.com/python-visualization/folium/main/examples/data"
14-
)
15-
us_states = f"{url}/us-states.json"
16-
17-
geo_json_data = json.loads(requests.get(us_states).text)
15+
geo_json_data = folium.example_data.us_states_geojson()
1816
```
1917

2018
It is a classical GeoJSON `FeatureCollection` (see https://en.wikipedia.org/wiki/GeoJSON) of the form :
@@ -44,14 +42,9 @@ It is a classical GeoJSON `FeatureCollection` (see https://en.wikipedia.org/wiki
4442
]
4543
}
4644

47-
+++
48-
4945
A first way of drawing it on a map, is simply to use `folium.GeoJson` :
5046

5147
```{code-cell} ipython3
52-
import folium
53-
54-
5548
m = folium.Map([43, -100], zoom_start=4)
5649
5750
folium.GeoJson(geo_json_data).add_to(m)
@@ -64,7 +57,9 @@ Note that you can avoid loading the file on yourself ; in simply providing a fil
6457
```{code-cell} ipython3
6558
m = folium.Map([43, -100], zoom_start=4)
6659
67-
folium.GeoJson(us_states).add_to(m)
60+
filepath = folium.example_data.get_path("us_states.json")
61+
62+
folium.GeoJson(filepath).add_to(m)
6863
6964
m
7065
```
@@ -74,7 +69,7 @@ You can pass a geopandas object.
7469
```{code-cell} ipython3
7570
import geopandas
7671
77-
gdf = geopandas.read_file(us_states)
72+
gdf = geopandas.read_file(filepath)
7873
7974
m = folium.Map([43, -100], zoom_start=4)
8075
@@ -153,10 +148,7 @@ Let's imagine we want to draw a choropleth of unemployment in the US.
153148
First, we may load the data:
154149

155150
```{code-cell} ipython3
156-
import pandas as pd
157-
158-
US_Unemployment_Oct2012 = f"{url}/US_Unemployment_Oct2012.csv"
159-
unemployment = pd.read_csv(US_Unemployment_Oct2012)
151+
unemployment = folium.example_data.us_unemployment_pandas_dataframe()
160152
161153
unemployment.head(5)
162154
```

docs/user_guide/geojson/geojson_marker.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# GeoJSON point features with markers
2-
31
```{code-cell} ipython3
4-
import os
2+
---
3+
nbsphinx: hidden
4+
---
55
import folium
6-
import geopandas as gpd
76
```
87

9-
```{code-cell} ipython3
10-
gdf = gpd.read_file("../../data/subwaystations.geojson")
11-
```
8+
# GeoJSON point features with markers
9+
1210

1311
```{code-cell} ipython3
12+
gdf = folium.example_data.subway_stations_geodataframe()
13+
1414
gdf.head()
1515
```
1616

0 commit comments

Comments
 (0)