GeoPandas extends pandas functionality to handle geographic and geospatial data operations with GeoSeries and GeoDataFrame classes.
—
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.
Methods for creating static maps using matplotlib backend.
class GeoDataFrame:
def plot(self, column=None, ax=None, figsize=None, color=None, colormap=None, vmin=None, vmax=None,
cmap=None, edgecolor=None, linewidth=None, markersize=None, alpha=None, legend=False,
legend_kwds=None, categorical=False, scheme=None, k=5, missing_kwds=None, **kwargs):
"""
Create static plot of geometries.
Parameters:
- column: Column name to use for coloring geometries
- ax: Matplotlib axes object to plot on
- figsize: Figure size as (width, height) tuple
- color: Single color for all geometries
- colormap: Colormap name or object for continuous data
- vmin: Minimum value for color scaling
- vmax: Maximum value for color scaling
- cmap: Alias for colormap parameter
- edgecolor: Color for geometry edges/borders
- linewidth: Width of geometry edges
- markersize: Size of point markers
- alpha: Transparency level (0-1)
- legend: Whether to show legend
- legend_kwds: Dictionary of legend parameters
- categorical: Whether to treat column data as categorical
- scheme: Classification scheme for continuous data
- k: Number of classes for classification schemes
- missing_kwds: Style parameters for missing values
- **kwargs: Additional matplotlib parameters
Returns:
- matplotlib.axes.Axes: Matplotlib axes object
"""
...
class GeoSeries:
def plot(self, ax=None, figsize=None, color=None, edgecolor=None, linewidth=None, markersize=None,
alpha=None, **kwargs):
"""
Create static plot of geometries.
Parameters:
- ax: Matplotlib axes object to plot on
- figsize: Figure size as (width, height) tuple
- color: Color for geometries
- edgecolor: Color for geometry edges/borders
- linewidth: Width of geometry edges
- markersize: Size of point markers
- alpha: Transparency level (0-1)
- **kwargs: Additional matplotlib parameters
Returns:
- matplotlib.axes.Axes: Matplotlib axes object
"""
...Methods for creating interactive maps using folium backend.
class GeoDataFrame:
def explore(self, column=None, cmap=None, color=None, m=None, tiles='OpenStreetMap',
attr=None, tooltip=True, popup=False, highlight=True, categorical=False,
legend=True, scheme=None, k=5, vmin=None, vmax=None, width='100%', height='100%',
categories=None, classification_kwds=None, control_scale=True, marker_type='marker',
marker_kwds=None, style_kwds=None, highlight_kwds=None, missing_kwds=None,
tooltip_kwds=None, popup_kwds=None, legend_kwds=None, **kwargs):
"""
Create interactive map using folium.
Parameters:
- column: Column name to use for coloring geometries
- cmap: Colormap for continuous data
- color: Single color for all geometries
- m: Existing folium Map object to add data to
- tiles: Tile layer ('OpenStreetMap', 'CartoDB positron', 'CartoDB dark_matter', etc.)
- attr: Attribution text for tiles
- tooltip: Whether to show tooltip on hover
- popup: Whether to show popup on click
- highlight: Whether to highlight geometries on hover
- categorical: Whether to treat column data as categorical
- legend: Whether to show legend
- scheme: Classification scheme for continuous data
- k: Number of classes for classification schemes
- vmin: Minimum value for color scaling
- vmax: Maximum value for color scaling
- width: Map width
- height: Map height
- categories: Explicit categories for categorical data
- classification_kwds: Parameters for classification
- control_scale: Whether to show scale control
- marker_type: Type of marker for points ('marker', 'circle_marker')
- marker_kwds: Style parameters for markers
- style_kwds: Style parameters for geometries
- highlight_kwds: Style parameters for highlighting
- missing_kwds: Style parameters for missing values
- tooltip_kwds: Parameters for tooltips
- popup_kwds: Parameters for popups
- legend_kwds: Parameters for legend
- **kwargs: Additional folium parameters
Returns:
- folium.Map: Interactive folium map
"""
...
class GeoSeries:
def explore(self, color=None, m=None, tiles='OpenStreetMap', attr=None, tooltip=True,
popup=False, highlight=True, control_scale=True, marker_type='marker',
marker_kwds=None, style_kwds=None, highlight_kwds=None, tooltip_kwds=None,
popup_kwds=None, **kwargs):
"""
Create interactive map using folium.
Parameters:
- color: Color for geometries
- m: Existing folium Map object to add data to
- tiles: Tile layer name
- attr: Attribution text for tiles
- tooltip: Whether to show tooltip on hover
- popup: Whether to show popup on click
- highlight: Whether to highlight geometries on hover
- control_scale: Whether to show scale control
- marker_type: Type of marker for points ('marker', 'circle_marker')
- marker_kwds: Style parameters for markers
- style_kwds: Style parameters for geometries
- highlight_kwds: Style parameters for highlighting
- tooltip_kwds: Parameters for tooltips
- popup_kwds: Parameters for popups
- **kwargs: Additional folium parameters
Returns:
- folium.Map: Interactive folium map
"""
...import geopandas as gpd
import matplotlib.pyplot as plt
# Load sample data
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Simple plot
world.plot()
plt.show()
# Customized plot
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
world.plot(ax=ax, color='lightblue', edgecolor='black', linewidth=0.5)
ax.set_title('World Map', fontsize=16)
plt.show()
# Plot with data-driven colors
world.plot(column='pop_est', cmap='OrRd', legend=True, figsize=(15, 10))
plt.title('World Population Estimates')
plt.show()# Choropleth map with custom classification
world.plot(column='gdp_md_est',
scheme='quantiles',
k=5,
cmap='plasma',
legend=True,
legend_kwds={'loc': 'lower left'},
figsize=(15, 10))
plt.title('World GDP Estimates (Quantile Classification)')
plt.axis('off') # Hide axes
plt.show()
# Multiple subplots
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
fig.suptitle('World Maps - Different Visualizations', fontsize=16)
# Population
world.plot(column='pop_est', cmap='Blues', ax=axes[0,0], legend=True)
axes[0,0].set_title('Population')
axes[0,0].axis('off')
# GDP
world.plot(column='gdp_md_est', cmap='Greens', ax=axes[0,1], legend=True)
axes[0,1].set_title('GDP')
axes[0,1].axis('off')
# Categorical data
world.plot(column='continent', categorical=True, ax=axes[1,0], legend=True)
axes[1,0].set_title('Continents')
axes[1,0].axis('off')
# Simple boundaries
world.plot(color='none', edgecolor='black', ax=axes[1,1])
axes[1,1].set_title('Boundaries Only')
axes[1,1].axis('off')
plt.tight_layout()
plt.show()# Create layered map with multiple datasets
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
# Base layer - countries
world.plot(ax=ax, color='lightgray', edgecolor='white', linewidth=0.5)
# Overlay - major cities
cities.plot(ax=ax, color='red', markersize=20, alpha=0.7)
ax.set_title('World Countries and Major Cities')
ax.axis('off')
plt.show()
# Focused regional map
# Filter data for specific region
europe = world[world['continent'] == 'Europe']
european_cities = cities[cities.within(europe.unary_union)]
fig, ax = plt.subplots(1, 1, figsize=(12, 8))
europe.plot(ax=ax, column='pop_est', cmap='viridis', legend=True)
european_cities.plot(ax=ax, color='red', markersize=50, alpha=0.8)
ax.set_title('European Population and Cities')
ax.axis('off')
plt.show()# Basic interactive map
m = world.explore()
m # Display in Jupyter notebook
# Interactive map with data-driven colors
m = world.explore(column='pop_est',
cmap='plasma',
legend=True,
tooltip=['name', 'pop_est'])
m
# Multiple tile options
tile_options = ['OpenStreetMap', 'CartoDB positron', 'CartoDB dark_matter', 'Stamen Terrain']
m = world.explore(column='gdp_md_est',
tiles='CartoDB positron',
cmap='viridis',
legend=True,
tooltip=['name', 'gdp_md_est'],
popup=['name', 'continent'])
m
# Add multiple layers to same map
m = world.explore(color='lightblue',
tooltip=['name'],
tiles='OpenStreetMap')
# Add cities as second layer
m = cities.explore(m=m, # Add to existing map
color='red',
marker_type='circle_marker',
tooltip=['name'],
marker_kwds={'radius': 5})
m# Categorical data with custom styling
m = world.explore(column='continent',
categorical=True,
tooltip=['name', 'continent'],
popup=['name', 'continent', 'pop_est'],
legend=True,
style_kwds={'fillOpacity': 0.7, 'weight': 2},
highlight_kwds={'weight': 4, 'fillOpacity': 0.9})
m
# Custom classification schemes
m = world.explore(column='pop_est',
scheme='natural_breaks', # Jenks natural breaks
k=7, # 7 classes
cmap='Reds',
legend=True,
tooltip=['name', 'pop_est'],
legend_kwds={'caption': 'Population (Natural Breaks)'})
m
# Points with different marker types
large_cities = cities[cities['pop_max'] > 5000000]
m = large_cities.explore(column='pop_max',
cmap='plasma',
marker_type='circle_marker',
tooltip=['name', 'pop_max'],
marker_kwds={'radius': 8},
legend=True)
m# Create static map for publication
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
world.plot(column='pop_est',
cmap='OrRd',
legend=True,
ax=ax,
legend_kwds={'shrink': 0.8})
cities.plot(ax=ax, color='blue', markersize=10, alpha=0.6)
ax.set_title('World Population and Major Cities', fontsize=16)
ax.axis('off')
plt.tight_layout()
plt.savefig('world_population_map.png', dpi=300, bbox_inches='tight')
plt.show()
# Create interactive version for web
m = world.explore(column='pop_est',
cmap='OrRd',
legend=True,
tooltip=['name', 'pop_est'],
style_kwds={'weight': 1})
m = cities.explore(m=m,
color='blue',
marker_type='circle_marker',
tooltip=['name'],
marker_kwds={'radius': 3, 'fillOpacity': 0.7})
# Save interactive map
m.save('world_population_interactive.html')
m# Custom color schemes and styling
import matplotlib.colors as colors
# Create custom colormap
custom_colors = ['#f7f7f7', '#cccccc', '#969696', '#636363', '#252525']
custom_cmap = colors.ListedColormap(custom_colors)
fig, ax = plt.subplots(1, 1, figsize=(15, 10))
world.plot(column='pop_est',
cmap=custom_cmap,
edgecolor='white',
linewidth=0.8,
ax=ax)
ax.set_facecolor('lightblue') # Ocean color
ax.set_title('World Population (Custom Colors)', fontsize=16)
plt.show()
# Interactive map with custom styling
style_dict = {
'fillColor': 'green',
'color': 'black',
'weight': 2,
'fillOpacity': 0.7
}
highlight_dict = {
'fillColor': 'red',
'color': 'white',
'weight': 3,
'fillOpacity': 0.9
}
m = world.explore(style_kwds=style_dict,
highlight_kwds=highlight_dict,
tooltip=['name'],
tiles='CartoDB dark_matter')
m# Different ways to visualize points
fig, axes = plt.subplots(2, 2, figsize=(15, 12))
# Simple points
cities.plot(ax=axes[0,0], color='red', markersize=20)
axes[0,0].set_title('Simple Points')
# Size by population
cities.plot(ax=axes[0,1],
markersize=cities['pop_max']/100000, # Scale population
color='blue', alpha=0.6)
axes[0,1].set_title('Sized by Population')
# Color by population
cities.plot(ax=axes[1,0],
column='pop_max',
cmap='viridis',
markersize=50,
legend=True)
axes[1,0].set_title('Colored by Population')
# Categorical coloring
cities.plot(ax=axes[1,1],
column='continent',
categorical=True,
markersize=30,
legend=True)
axes[1,1].set_title('Colored by Continent')
for ax in axes.flat:
ax.axis('off')
plt.tight_layout()
plt.show()Install with Tessl CLI
npx tessl i tessl/pypi-geopandas