Project 39: Choropleth Lite 🌍¶

Difficulty: 🔴 Advanced

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/choropleth_lite/.

← Back to index

Description¶

Build a lightweight category map with Folium circle markers sized by a fun dataset (no GeoJSON needed).

Libraries & Modules¶

  • folium
  • pandas

Python Concepts You'll Practice¶

Geodata tables, interactive HTML maps, marker styling

🎛️ Parameter Variations¶

  • City list + values
  • Color thresholds
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "choropleth_lite"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\choropleth_lite

Step 1 — Invent city energy scores¶

In [3]:
import pandas as pd

# 🎛️ TWEAK THESE
DF = pd.DataFrame([
    {"city": "Tokyo", "lat": 35.68, "lon": 139.65, "energy": 92},
    {"city": "Lagos", "lat": 6.52, "lon": 3.38, "energy": 80},
    {"city": "São Paulo", "lat": -23.55, "lon": -46.63, "energy": 77},
    {"city": "Berlin", "lat": 52.52, "lon": 13.40, "energy": 70},
    {"city": "Seoul", "lat": 37.57, "lon": 126.98, "energy": 88},
    {"city": "Mexico City", "lat": 19.43, "lon": -99.13, "energy": 74},
])
print(DF)
          city    lat     lon  energy
0        Tokyo  35.68  139.65      92
1        Lagos   6.52    3.38      80
2    São Paulo -23.55  -46.63      77
3       Berlin  52.52   13.40      70
4        Seoul  37.57  126.98      88
5  Mexico City  19.43  -99.13      74

Step 2 — Folium bubble map¶

In [4]:
import folium

m = folium.Map(location=[20, 10], zoom_start=2, tiles="CartoDB positron")
for _, r in DF.iterrows():
    color = "#8b1e1e" if r.energy >= 85 else "#333333" if r.energy >= 75 else "#999999"
    folium.CircleMarker(
        location=[r.lat, r.lon],
        radius=r.energy / 8,
        color=color,
        fill=True,
        fill_opacity=0.65,
        popup=f"{r.city}: {r.energy}",
    ).add_to(m)
out = OUTPUT_DIR / "choropleth_lite.html"
m.save(str(out))
print(f"Map saved → {out}")
display(m)
Map saved → outputs\choropleth_lite\choropleth_lite.html
Make this Notebook Trusted to load map: File -> Trust Notebook

✅ Project complete¶

Check your output folder: outputs/choropleth_lite/

Next: Project 40: Neural Net From Scratch Lite · Index