0
# Visualization
1
2
GeoPandas provides comprehensive visualization capabilities for creating both static and interactive maps. The visualization system integrates with matplotlib for publication-quality static maps and folium for interactive web-based visualizations.
3
4
## Capabilities
5
6
### Static Plotting
7
8
Methods for creating static maps using matplotlib backend.
9
10
```python { .api }
11
class GeoDataFrame:
12
def plot(self, column=None, ax=None, figsize=None, color=None, colormap=None, vmin=None, vmax=None,
13
cmap=None, edgecolor=None, linewidth=None, markersize=None, alpha=None, legend=False,
14
legend_kwds=None, categorical=False, scheme=None, k=5, missing_kwds=None, **kwargs):
15
"""
16
Create static plot of geometries.
17
18
Parameters:
19
- column: Column name to use for coloring geometries
20
- ax: Matplotlib axes object to plot on
21
- figsize: Figure size as (width, height) tuple
22
- color: Single color for all geometries
23
- colormap: Colormap name or object for continuous data
24
- vmin: Minimum value for color scaling
25
- vmax: Maximum value for color scaling
26
- cmap: Alias for colormap parameter
27
- edgecolor: Color for geometry edges/borders
28
- linewidth: Width of geometry edges
29
- markersize: Size of point markers
30
- alpha: Transparency level (0-1)
31
- legend: Whether to show legend
32
- legend_kwds: Dictionary of legend parameters
33
- categorical: Whether to treat column data as categorical
34
- scheme: Classification scheme for continuous data
35
- k: Number of classes for classification schemes
36
- missing_kwds: Style parameters for missing values
37
- **kwargs: Additional matplotlib parameters
38
39
Returns:
40
- matplotlib.axes.Axes: Matplotlib axes object
41
"""
42
...
43
44
class GeoSeries:
45
def plot(self, ax=None, figsize=None, color=None, edgecolor=None, linewidth=None, markersize=None,
46
alpha=None, **kwargs):
47
"""
48
Create static plot of geometries.
49
50
Parameters:
51
- ax: Matplotlib axes object to plot on
52
- figsize: Figure size as (width, height) tuple
53
- color: Color for geometries
54
- edgecolor: Color for geometry edges/borders
55
- linewidth: Width of geometry edges
56
- markersize: Size of point markers
57
- alpha: Transparency level (0-1)
58
- **kwargs: Additional matplotlib parameters
59
60
Returns:
61
- matplotlib.axes.Axes: Matplotlib axes object
62
"""
63
...
64
```
65
66
### Interactive Mapping
67
68
Methods for creating interactive maps using folium backend.
69
70
```python { .api }
71
class GeoDataFrame:
72
def explore(self, column=None, cmap=None, color=None, m=None, tiles='OpenStreetMap',
73
attr=None, tooltip=True, popup=False, highlight=True, categorical=False,
74
legend=True, scheme=None, k=5, vmin=None, vmax=None, width='100%', height='100%',
75
categories=None, classification_kwds=None, control_scale=True, marker_type='marker',
76
marker_kwds=None, style_kwds=None, highlight_kwds=None, missing_kwds=None,
77
tooltip_kwds=None, popup_kwds=None, legend_kwds=None, **kwargs):
78
"""
79
Create interactive map using folium.
80
81
Parameters:
82
- column: Column name to use for coloring geometries
83
- cmap: Colormap for continuous data
84
- color: Single color for all geometries
85
- m: Existing folium Map object to add data to
86
- tiles: Tile layer ('OpenStreetMap', 'CartoDB positron', 'CartoDB dark_matter', etc.)
87
- attr: Attribution text for tiles
88
- tooltip: Whether to show tooltip on hover
89
- popup: Whether to show popup on click
90
- highlight: Whether to highlight geometries on hover
91
- categorical: Whether to treat column data as categorical
92
- legend: Whether to show legend
93
- scheme: Classification scheme for continuous data
94
- k: Number of classes for classification schemes
95
- vmin: Minimum value for color scaling
96
- vmax: Maximum value for color scaling
97
- width: Map width
98
- height: Map height
99
- categories: Explicit categories for categorical data
100
- classification_kwds: Parameters for classification
101
- control_scale: Whether to show scale control
102
- marker_type: Type of marker for points ('marker', 'circle_marker')
103
- marker_kwds: Style parameters for markers
104
- style_kwds: Style parameters for geometries
105
- highlight_kwds: Style parameters for highlighting
106
- missing_kwds: Style parameters for missing values
107
- tooltip_kwds: Parameters for tooltips
108
- popup_kwds: Parameters for popups
109
- legend_kwds: Parameters for legend
110
- **kwargs: Additional folium parameters
111
112
Returns:
113
- folium.Map: Interactive folium map
114
"""
115
...
116
117
class GeoSeries:
118
def explore(self, color=None, m=None, tiles='OpenStreetMap', attr=None, tooltip=True,
119
popup=False, highlight=True, control_scale=True, marker_type='marker',
120
marker_kwds=None, style_kwds=None, highlight_kwds=None, tooltip_kwds=None,
121
popup_kwds=None, **kwargs):
122
"""
123
Create interactive map using folium.
124
125
Parameters:
126
- color: Color for geometries
127
- m: Existing folium Map object to add data to
128
- tiles: Tile layer name
129
- attr: Attribution text for tiles
130
- tooltip: Whether to show tooltip on hover
131
- popup: Whether to show popup on click
132
- highlight: Whether to highlight geometries on hover
133
- control_scale: Whether to show scale control
134
- marker_type: Type of marker for points ('marker', 'circle_marker')
135
- marker_kwds: Style parameters for markers
136
- style_kwds: Style parameters for geometries
137
- highlight_kwds: Style parameters for highlighting
138
- tooltip_kwds: Parameters for tooltips
139
- popup_kwds: Parameters for popups
140
- **kwargs: Additional folium parameters
141
142
Returns:
143
- folium.Map: Interactive folium map
144
"""
145
...
146
```
147
148
## Usage Examples
149
150
### Basic Static Plots
151
152
```python
153
import geopandas as gpd
154
import matplotlib.pyplot as plt
155
156
# Load sample data
157
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
158
159
# Simple plot
160
world.plot()
161
plt.show()
162
163
# Customized plot
164
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
165
world.plot(ax=ax, color='lightblue', edgecolor='black', linewidth=0.5)
166
ax.set_title('World Map', fontsize=16)
167
plt.show()
168
169
# Plot with data-driven colors
170
world.plot(column='pop_est', cmap='OrRd', legend=True, figsize=(15, 10))
171
plt.title('World Population Estimates')
172
plt.show()
173
```
174
175
### Advanced Static Plotting
176
177
```python
178
# Choropleth map with custom classification
179
world.plot(column='gdp_md_est',
180
scheme='quantiles',
181
k=5,
182
cmap='plasma',
183
legend=True,
184
legend_kwds={'loc': 'lower left'},
185
figsize=(15, 10))
186
plt.title('World GDP Estimates (Quantile Classification)')
187
plt.axis('off') # Hide axes
188
plt.show()
189
190
# Multiple subplots
191
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
192
fig.suptitle('World Maps - Different Visualizations', fontsize=16)
193
194
# Population
195
world.plot(column='pop_est', cmap='Blues', ax=axes[0,0], legend=True)
196
axes[0,0].set_title('Population')
197
axes[0,0].axis('off')
198
199
# GDP
200
world.plot(column='gdp_md_est', cmap='Greens', ax=axes[0,1], legend=True)
201
axes[0,1].set_title('GDP')
202
axes[0,1].axis('off')
203
204
# Categorical data
205
world.plot(column='continent', categorical=True, ax=axes[1,0], legend=True)
206
axes[1,0].set_title('Continents')
207
axes[1,0].axis('off')
208
209
# Simple boundaries
210
world.plot(color='none', edgecolor='black', ax=axes[1,1])
211
axes[1,1].set_title('Boundaries Only')
212
axes[1,1].axis('off')
213
214
plt.tight_layout()
215
plt.show()
216
```
217
218
### Layered Static Maps
219
220
```python
221
# Create layered map with multiple datasets
222
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
223
224
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
225
226
# Base layer - countries
227
world.plot(ax=ax, color='lightgray', edgecolor='white', linewidth=0.5)
228
229
# Overlay - major cities
230
cities.plot(ax=ax, color='red', markersize=20, alpha=0.7)
231
232
ax.set_title('World Countries and Major Cities')
233
ax.axis('off')
234
plt.show()
235
236
# Focused regional map
237
# Filter data for specific region
238
europe = world[world['continent'] == 'Europe']
239
european_cities = cities[cities.within(europe.unary_union)]
240
241
fig, ax = plt.subplots(1, 1, figsize=(12, 8))
242
europe.plot(ax=ax, column='pop_est', cmap='viridis', legend=True)
243
european_cities.plot(ax=ax, color='red', markersize=50, alpha=0.8)
244
ax.set_title('European Population and Cities')
245
ax.axis('off')
246
plt.show()
247
```
248
249
### Interactive Maps
250
251
```python
252
# Basic interactive map
253
m = world.explore()
254
m # Display in Jupyter notebook
255
256
# Interactive map with data-driven colors
257
m = world.explore(column='pop_est',
258
cmap='plasma',
259
legend=True,
260
tooltip=['name', 'pop_est'])
261
m
262
263
# Multiple tile options
264
tile_options = ['OpenStreetMap', 'CartoDB positron', 'CartoDB dark_matter', 'Stamen Terrain']
265
266
m = world.explore(column='gdp_md_est',
267
tiles='CartoDB positron',
268
cmap='viridis',
269
legend=True,
270
tooltip=['name', 'gdp_md_est'],
271
popup=['name', 'continent'])
272
m
273
274
# Add multiple layers to same map
275
m = world.explore(color='lightblue',
276
tooltip=['name'],
277
tiles='OpenStreetMap')
278
279
# Add cities as second layer
280
m = cities.explore(m=m, # Add to existing map
281
color='red',
282
marker_type='circle_marker',
283
tooltip=['name'],
284
marker_kwds={'radius': 5})
285
m
286
```
287
288
### Advanced Interactive Features
289
290
```python
291
# Categorical data with custom styling
292
m = world.explore(column='continent',
293
categorical=True,
294
tooltip=['name', 'continent'],
295
popup=['name', 'continent', 'pop_est'],
296
legend=True,
297
style_kwds={'fillOpacity': 0.7, 'weight': 2},
298
highlight_kwds={'weight': 4, 'fillOpacity': 0.9})
299
m
300
301
# Custom classification schemes
302
m = world.explore(column='pop_est',
303
scheme='natural_breaks', # Jenks natural breaks
304
k=7, # 7 classes
305
cmap='Reds',
306
legend=True,
307
tooltip=['name', 'pop_est'],
308
legend_kwds={'caption': 'Population (Natural Breaks)'})
309
m
310
311
# Points with different marker types
312
large_cities = cities[cities['pop_max'] > 5000000]
313
314
m = large_cities.explore(column='pop_max',
315
cmap='plasma',
316
marker_type='circle_marker',
317
tooltip=['name', 'pop_max'],
318
marker_kwds={'radius': 8},
319
legend=True)
320
m
321
```
322
323
### Combining Static and Interactive
324
325
```python
326
# Create static map for publication
327
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
328
world.plot(column='pop_est',
329
cmap='OrRd',
330
legend=True,
331
ax=ax,
332
legend_kwds={'shrink': 0.8})
333
cities.plot(ax=ax, color='blue', markersize=10, alpha=0.6)
334
ax.set_title('World Population and Major Cities', fontsize=16)
335
ax.axis('off')
336
plt.tight_layout()
337
plt.savefig('world_population_map.png', dpi=300, bbox_inches='tight')
338
plt.show()
339
340
# Create interactive version for web
341
m = world.explore(column='pop_est',
342
cmap='OrRd',
343
legend=True,
344
tooltip=['name', 'pop_est'],
345
style_kwds={'weight': 1})
346
347
m = cities.explore(m=m,
348
color='blue',
349
marker_type='circle_marker',
350
tooltip=['name'],
351
marker_kwds={'radius': 3, 'fillOpacity': 0.7})
352
353
# Save interactive map
354
m.save('world_population_interactive.html')
355
m
356
```
357
358
### Customizing Map Appearance
359
360
```python
361
# Custom color schemes and styling
362
import matplotlib.colors as colors
363
364
# Create custom colormap
365
custom_colors = ['#f7f7f7', '#cccccc', '#969696', '#636363', '#252525']
366
custom_cmap = colors.ListedColormap(custom_colors)
367
368
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
369
world.plot(column='pop_est',
370
cmap=custom_cmap,
371
edgecolor='white',
372
linewidth=0.8,
373
ax=ax)
374
ax.set_facecolor('lightblue') # Ocean color
375
ax.set_title('World Population (Custom Colors)', fontsize=16)
376
plt.show()
377
378
# Interactive map with custom styling
379
style_dict = {
380
'fillColor': 'green',
381
'color': 'black',
382
'weight': 2,
383
'fillOpacity': 0.7
384
}
385
386
highlight_dict = {
387
'fillColor': 'red',
388
'color': 'white',
389
'weight': 3,
390
'fillOpacity': 0.9
391
}
392
393
m = world.explore(style_kwds=style_dict,
394
highlight_kwds=highlight_dict,
395
tooltip=['name'],
396
tiles='CartoDB dark_matter')
397
m
398
```
399
400
### Working with Point Data
401
402
```python
403
# Different ways to visualize points
404
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
405
406
# Simple points
407
cities.plot(ax=axes[0,0], color='red', markersize=20)
408
axes[0,0].set_title('Simple Points')
409
410
# Size by population
411
cities.plot(ax=axes[0,1],
412
markersize=cities['pop_max']/100000, # Scale population
413
color='blue', alpha=0.6)
414
axes[0,1].set_title('Sized by Population')
415
416
# Color by population
417
cities.plot(ax=axes[1,0],
418
column='pop_max',
419
cmap='viridis',
420
markersize=50,
421
legend=True)
422
axes[1,0].set_title('Colored by Population')
423
424
# Categorical coloring
425
cities.plot(ax=axes[1,1],
426
column='continent',
427
categorical=True,
428
markersize=30,
429
legend=True)
430
axes[1,1].set_title('Colored by Continent')
431
432
for ax in axes.flat:
433
ax.axis('off')
434
435
plt.tight_layout()
436
plt.show()
437
```