Choropleth Maps¶
This notebook demonstrates how to create choropleth (thematic) maps with automatic data classification and color mapping.
In [ ]:
Copied!
# %pip install anymap-ts
# %pip install anymap-ts
Basic Choropleth with US States¶
In [ ]:
Copied!
from anymap_ts import Map
# US states GeoJSON with population data
states_url = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
m = Map(center=[-98, 38], zoom=3)
m.add_choropleth(
states_url,
column="density",
cmap="YlOrRd",
classification="quantile",
k=5,
legend_title="Population Density",
)
m
from anymap_ts import Map
# US states GeoJSON with population data
states_url = "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
m = Map(center=[-98, 38], zoom=3)
m.add_choropleth(
states_url,
column="density",
cmap="YlOrRd",
classification="quantile",
k=5,
legend_title="Population Density",
)
m
In [ ]:
Copied!
m2 = Map(center=[-98, 38], zoom=3)
m2.add_choropleth(
states_url,
column="density",
cmap="Blues",
classification="quantile",
k=5,
legend_title="Density (Quantile)",
)
m2
m2 = Map(center=[-98, 38], zoom=3)
m2.add_choropleth(
states_url,
column="density",
cmap="Blues",
classification="quantile",
k=5,
legend_title="Density (Quantile)",
)
m2
Equal Interval Classification¶
Equal interval divides the data range into equal-width bins.
Note: Equal interval can produce poor results with skewed data (like population density where most values are low). In such cases, most features end up in the first class. Use quantile or manual breaks for skewed data.
In [ ]:
Copied!
# Equal interval works better with more uniformly distributed data
# For skewed data like density, consider using manual breaks
m3 = Map(center=[-98, 38], zoom=3)
m3.add_choropleth(
states_url,
column="density",
cmap="Greens",
classification="manual",
breaks=[0, 25, 75, 150, 300, 1200], # Custom breaks for better distribution
k=5,
legend_title="Density (Custom Equal-ish)",
)
m3
# Equal interval works better with more uniformly distributed data
# For skewed data like density, consider using manual breaks
m3 = Map(center=[-98, 38], zoom=3)
m3.add_choropleth(
states_url,
column="density",
cmap="Greens",
classification="manual",
breaks=[0, 25, 75, 150, 300, 1200], # Custom breaks for better distribution
k=5,
legend_title="Density (Custom Equal-ish)",
)
m3
More Classes with Quantile¶
In [ ]:
Copied!
m4 = Map(center=[-98, 38], zoom=3)
m4.add_choropleth(
states_url,
column="density",
cmap="Oranges",
classification="quantile",
k=7,
legend_title="Density (Quantile, 7 classes)",
)
m4
m4 = Map(center=[-98, 38], zoom=3)
m4.add_choropleth(
states_url,
column="density",
cmap="Oranges",
classification="quantile",
k=7,
legend_title="Density (Quantile, 7 classes)",
)
m4
Different Color Maps¶
Any matplotlib colormap is supported. Common options include:
- Sequential: viridis, plasma, inferno, Blues, Greens, Reds, Purples
- Diverging: RdBu, RdYlGn, Spectral, coolwarm, seismic
- Qualitative: Set1, Set2, tab10, Paired
In [ ]:
Copied!
# Viridis colormap (perceptually uniform)
m5 = Map(center=[-98, 38], zoom=3)
m5.add_choropleth(
states_url, column="density", cmap="viridis", k=6, legend_title="Density (Viridis)"
)
m5
# Viridis colormap (perceptually uniform)
m5 = Map(center=[-98, 38], zoom=3)
m5.add_choropleth(
states_url, column="density", cmap="viridis", k=6, legend_title="Density (Viridis)"
)
m5
In [ ]:
Copied!
# Spectral colormap (diverging - good for showing deviation from middle)
m6 = Map(center=[-98, 38], zoom=3)
m6.add_choropleth(
states_url,
column="density",
cmap="Spectral",
k=5,
legend_title="Density (Spectral)",
)
m6
# Spectral colormap (diverging - good for showing deviation from middle)
m6 = Map(center=[-98, 38], zoom=3)
m6.add_choropleth(
states_url,
column="density",
cmap="Spectral",
k=5,
legend_title="Density (Spectral)",
)
m6
In [ ]:
Copied!
# Plasma colormap
m7 = Map(center=[-98, 38], zoom=3)
m7.add_choropleth(
states_url, column="density", cmap="plasma", k=5, legend_title="Density (Plasma)"
)
m7
# Plasma colormap
m7 = Map(center=[-98, 38], zoom=3)
m7.add_choropleth(
states_url, column="density", cmap="plasma", k=5, legend_title="Density (Plasma)"
)
m7
Custom Styling¶
In [ ]:
Copied!
m8 = Map(center=[-98, 38], zoom=3)
m8.add_basemap("CartoDB.DarkMatter")
m8.add_choropleth(
states_url,
column="density",
cmap="inferno",
classification="quantile",
k=5,
fill_opacity=0.8,
line_color="#ffffff",
line_width=2,
legend_title="Population Density",
)
m8
m8 = Map(center=[-98, 38], zoom=3)
m8.add_basemap("CartoDB.DarkMatter")
m8.add_choropleth(
states_url,
column="density",
cmap="inferno",
classification="quantile",
k=5,
fill_opacity=0.8,
line_color="#ffffff",
line_width=2,
legend_title="Population Density",
)
m8
Manual Classification Breaks¶
For full control, specify exact break values. This is useful when you have meaningful thresholds (e.g., low/medium/high categories).
In [ ]:
Copied!
m9 = Map(center=[-98, 38], zoom=3)
m9.add_choropleth(
states_url,
column="density",
cmap="RdYlGn",
classification="manual",
breaks=[0, 20, 50, 100, 250, 1200], # 5 classes with meaningful thresholds
k=5,
legend_title="Custom Breaks",
)
m9
m9 = Map(center=[-98, 38], zoom=3)
m9.add_choropleth(
states_url,
column="density",
cmap="RdYlGn",
classification="manual",
breaks=[0, 20, 50, 100, 250, 1200], # 5 classes with meaningful thresholds
k=5,
legend_title="Custom Breaks",
)
m9
Choropleth without Legend¶
In [ ]:
Copied!
m10 = Map(center=[-98, 38], zoom=3)
m10.add_choropleth(
states_url, column="density", cmap="coolwarm", k=5, legend=False # No legend
)
m10
m10 = Map(center=[-98, 38], zoom=3)
m10.add_choropleth(
states_url, column="density", cmap="coolwarm", k=5, legend=False # No legend
)
m10
Choropleth with Hover Disabled¶
In [ ]:
Copied!
m11 = Map(center=[-98, 38], zoom=3)
m11.add_choropleth(
states_url,
column="density",
cmap="Purples",
k=5,
hover=False, # Disable hover highlight
legend_title="No Hover Effect",
)
m11
m11 = Map(center=[-98, 38], zoom=3)
m11.add_choropleth(
states_url,
column="density",
cmap="Purples",
k=5,
hover=False, # Disable hover highlight
legend_title="No Hover Effect",
)
m11
Export to HTML¶
In [ ]:
Copied!
m.to_html("choropleth_example.html")
m.to_html("choropleth_example.html")