-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython_geopandas_folium_choropleths.qmd
205 lines (156 loc) · 6.07 KB
/
python_geopandas_folium_choropleths.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Interactive Maps - Choropleths
```{r, echo = FALSE}
source("glossary_funcs.R")
glossary_setup()
```
```{r, echo = FALSE, results='asis'}
glossary_setup_style()
```
Let's first load in, filter and inspect the dataset we will be using for these choropleths.
```{python}
import pandas as pd
import geopandas
lsoa_2011_crime_figures_df = geopandas.read_file("https://github.com/hsma-programme/h6_3b_advanced_qgis_mapping_python/raw/main/h6_3b_advanced_qgis_and_mapping_in_python/example_code/lsoa_2011_sw5forces_crime_figures.gpkg")
lsoa_2011_crime_figures_df_devon=lsoa_2011_crime_figures_df.cx[250000:350000, 0:150000]
lsoa_2011_crime_figures_df.head()
```
## Creating choropleths
We can also create choropleths in Folium.
As with point data, we start by importing Folium and creating a basemap.
We then create a new choropleth layer and add this to the basemap.
```{python}
import folium
#create base map
bike_crime_map_interactive = folium.Map(
location=[50.71671, -3.50668],
zoom_start=9,
tiles='cartodbpositron'
)
# create and add choropleth map
choropleth = folium.Choropleth(
geo_data=lsoa_2011_crime_figures_df_devon, # dataframe with geometry in it
data=lsoa_2011_crime_figures_df_devon, # dataframe with data in - may be the same dataframe or a different one
columns=['LSOA11NM', 'sw_5forces_street_by_lsoa_Bicycle theft'], # [key (field for geometry), field to plot]
key_on='feature.properties.LSOA11NM'
)
choropleth = choropleth.add_to(bike_crime_map_interactive)
bike_crime_map_interactive
```
### Adding complexity
We can pass in additional arguments to the `folium.Choropleth()` call to tweak the display further.
`fill_colour` takes a matplotlib colourmap string to change the colours used for the scale.
`fill_opacity` affects how see-through (transparent) the choropleth layer is. This is a value between 0 and 1, with 1 being totally opaque and 0 being totally see-through.
`line_weight` affects the thickness of the outlines around different boundaries.
`legend_name` adjusts the label attached to the legend.
`highlight` highlights the LSOA shape when mouse pointer enters it if set to `True`; it defaults to `False`.
`smooth_factor` affects how simplified the boundaries of each region will be; 0 ensures no simplification occurs.
```{python}
#create base map
bike_crime_map_interactive = folium.Map(
location=[50.71671, -3.50668],
zoom_start=9,
tiles='cartodbpositron'
)
# create and add choropleth map
choropleth = folium.Choropleth(
geo_data=lsoa_2011_crime_figures_df_devon, # dataframe with geometry in it
data=lsoa_2011_crime_figures_df_devon, # dataframe with data in - may be the same dataframe or a different one
columns=['LSOA11NM', 'sw_5forces_street_by_lsoa_Bicycle theft'], # [key (field for geometry), field to plot]
key_on='feature.properties.LSOA11NM',
fill_color='OrRd',
fill_opacity=0.4,
line_weight=0.3,
legend_name='Bicycle Thefts',
highlight=True, # highlight the LSOA shape when mouse pointer enters it
smooth_factor=0
)
choropleth = choropleth.add_to(bike_crime_map_interactive)
bike_crime_map_interactive
```
## Tooltips
Simple tooltips can be added with this code, passing in the column of interest as well as the region label:
```{python}
#| eval: True
choropleth = choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(
['LSOA11NM', 'sw_5forces_street_by_lsoa_Bicycle theft'],
labels=True
)
)
```
Here is the full code.
```{python}
#create base map
bike_crime_map_interactive = folium.Map(
location=[50.71671, -3.50668],
zoom_start=9,
tiles='cartodbpositron'
)
# create and add choropleth map
choropleth = folium.Choropleth(
geo_data=lsoa_2011_crime_figures_df_devon, # dataframe with geometry in it
data=lsoa_2011_crime_figures_df_devon, # dataframe with data in - may be the same dataframe or a different one
columns=['LSOA11NM', 'sw_5forces_street_by_lsoa_Bicycle theft'], # [key (field for geometry), field to plot]
key_on='feature.properties.LSOA11NM',
fill_color='OrRd',
fill_opacity=0.4,
line_weight=0.3,
legend_name='Bicycle Thefts',
highlight=True, # highlight the LSOA shape when mouse pointer enters it
smooth_factor=0
)
choropleth = choropleth.add_to(bike_crime_map_interactive)
choropleth = choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(
['LSOA11NM', 'sw_5forces_street_by_lsoa_Bicycle theft'],
labels=True
)
)
bike_crime_map_interactive
```
### Adding additional columns to choropleths
By adding additional columns to the list we pass in to the `folium.features.GeoJsonTooltip()` function, we can include as many additional columns as we would like in our choropleth.
For example:
```{python}
#| eval: False
choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(
['LSOA11NM',
'sw_5forces_street_by_lsoa_Bicycle theft',
'sw_5forces_street_by_lsoa_Total number crimes'],
labels=True
)
)
```
Here is the full code.
```{python}
#create base map
bike_crime_map_interactive = folium.Map(
location=[50.71671, -3.50668],
zoom_start=9,
tiles='cartodbpositron'
)
# create and add choropleth map
choropleth = folium.Choropleth(
geo_data=lsoa_2011_crime_figures_df_devon, # dataframe with geometry in it
data=lsoa_2011_crime_figures_df_devon, # dataframe with data in - may be the same dataframe or a different one
columns=['LSOA11NM', 'sw_5forces_street_by_lsoa_Bicycle theft'], # [key (field for geometry), field to plot]
key_on='feature.properties.LSOA11NM',
fill_color='OrRd',
fill_opacity=0.4,
line_weight=0.3,
legend_name='Bicycle Thefts',
highlight=True, # highlight the LSOA shape when mouse pointer enters it
smooth_factor=0
)
choropleth = choropleth.add_to(bike_crime_map_interactive)
choropleth = choropleth.geojson.add_child(
folium.features.GeoJsonTooltip(
['LSOA11NM',
'sw_5forces_street_by_lsoa_Bicycle theft',
'sw_5forces_street_by_lsoa_Total number crimes'],
labels=True
)
)
bike_crime_map_interactive
```