Skip to content

Commit eeca6cd

Browse files
committed
Add contextily road basemaps to NB02 and NB05 geographic plots
NB02: Block group boundary map now shows street tiles behind the census polygons and isochrone overlay (Austin, TX). NB05: POI scatter plot now shows street tiles behind the category- colored markers (Seattle, WA).
1 parent 2a0fb0b commit eeca6cd

2 files changed

Lines changed: 5 additions & 109 deletions

File tree

docs/notebooks/02-census-block-groups.ipynb

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,7 @@
6868
"execution_count": null,
6969
"metadata": {},
7070
"outputs": [],
71-
"source": [
72-
"# Uncomment to install on Google Colab:\n",
73-
"# !pip install 'socialmapper @ git+https://github.com/mihiarc/socialmapper.git'\n",
74-
"\n",
75-
"from socialmapper import create_isochrone, get_census_blocks\n",
76-
"\n",
77-
"import matplotlib.pyplot as plt\n",
78-
"import pandas as pd"
79-
]
71+
"source": "# Uncomment to install on Google Colab:\n# !pip install 'socialmapper @ git+https://github.com/mihiarc/socialmapper.git'\n\nfrom socialmapper import create_isochrone, get_census_blocks\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport geopandas as gpd\nimport contextily as cx"
8072
},
8173
{
8274
"cell_type": "markdown",
@@ -248,47 +240,7 @@
248240
"execution_count": null,
249241
"metadata": {},
250242
"outputs": [],
251-
"source": [
252-
"from shapely.geometry import shape\n",
253-
"\n",
254-
"fig, ax = plt.subplots(figsize=(10, 10))\n",
255-
"\n",
256-
"# Collect areas for the colormap\n",
257-
"areas = [b[\"area_sq_km\"] for b in blocks]\n",
258-
"norm = plt.Normalize(vmin=min(areas), vmax=max(areas))\n",
259-
"cmap = plt.cm.YlOrRd\n",
260-
"\n",
261-
"# Plot each block group polygon\n",
262-
"for b in blocks:\n",
263-
" geom = shape(b[\"geometry\"])\n",
264-
" color = cmap(norm(b[\"area_sq_km\"]))\n",
265-
" if geom.geom_type == \"Polygon\":\n",
266-
" xs, ys = geom.exterior.xy\n",
267-
" ax.fill(xs, ys, alpha=0.6, fc=color, ec=\"#555555\", linewidth=0.5)\n",
268-
" elif geom.geom_type == \"MultiPolygon\":\n",
269-
" for poly in geom.geoms:\n",
270-
" xs, ys = poly.exterior.xy\n",
271-
" ax.fill(xs, ys, alpha=0.6, fc=color, ec=\"#555555\", linewidth=0.5)\n",
272-
"\n",
273-
"# Overlay the isochrone boundary\n",
274-
"iso_geom = shape(iso[\"geometry\"])\n",
275-
"xs, ys = iso_geom.exterior.xy\n",
276-
"ax.plot(xs, ys, color=\"#d63384\", linewidth=2.5, linestyle=\"--\", label=\"15-min drive isochrone\")\n",
277-
"\n",
278-
"# Colorbar\n",
279-
"sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)\n",
280-
"sm.set_array([])\n",
281-
"cbar = plt.colorbar(sm, ax=ax, shrink=0.7, pad=0.02)\n",
282-
"cbar.set_label(\"Area (sq km)\", fontsize=11)\n",
283-
"\n",
284-
"ax.set_title(\"Census Block Groups within 15-min Drive of Austin, TX\", fontsize=14, fontweight=\"bold\", color=\"#1a1a2e\")\n",
285-
"ax.set_xlabel(\"Longitude\")\n",
286-
"ax.set_ylabel(\"Latitude\")\n",
287-
"ax.legend(loc=\"upper right\", fontsize=10)\n",
288-
"ax.set_aspect(\"equal\")\n",
289-
"plt.tight_layout()\n",
290-
"plt.show()"
291-
]
243+
"source": "from shapely.geometry import shape\n\nfig, ax = plt.subplots(figsize=(10, 10))\n\n# Build a GeoDataFrame of block group polygons with area data\ngeometries = [shape(b[\"geometry\"]) for b in blocks]\nareas = [b[\"area_sq_km\"] for b in blocks]\ngdf_blocks = gpd.GeoDataFrame({\"area_sq_km\": areas}, geometry=geometries, crs=\"EPSG:4326\")\n\n# Plot block groups colored by area\ngdf_blocks.plot(\n ax=ax, column=\"area_sq_km\", cmap=\"YlOrRd\", alpha=0.6,\n edgecolor=\"#555555\", linewidth=0.5, legend=True,\n legend_kwds={\"label\": \"Area (sq km)\", \"shrink\": 0.7, \"pad\": 0.02},\n)\n\n# Overlay the isochrone boundary\niso_geom = shape(iso[\"geometry\"])\ngdf_iso = gpd.GeoDataFrame(geometry=[iso_geom], crs=\"EPSG:4326\")\ngdf_iso.boundary.plot(ax=ax, color=\"#d63384\", linewidth=2.5, linestyle=\"--\", label=\"15-min drive isochrone\")\n\n# Add road basemap\ncx.add_basemap(ax, crs=\"EPSG:4326\", source=cx.providers.CartoDB.Positron)\n\nax.set_title(\"Census Block Groups within 15-min Drive of Austin, TX\", fontsize=14, fontweight=\"bold\", color=\"#1a1a2e\")\nax.set_xlabel(\"Longitude\")\nax.set_ylabel(\"Latitude\")\nax.legend(loc=\"upper right\", fontsize=10)\nplt.tight_layout()\nplt.show()"
292244
},
293245
{
294246
"cell_type": "markdown",
@@ -559,4 +511,4 @@
559511
},
560512
"nbformat": 4,
561513
"nbformat_minor": 4
562-
}
514+
}

docs/notebooks/05-points-of-interest.ipynb

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,7 @@
4242
"id": "cell-2",
4343
"metadata": {},
4444
"outputs": [],
45-
"source": [
46-
"# Uncomment to install on Google Colab:\n",
47-
"# !pip install 'socialmapper @ git+https://github.com/mihiarc/socialmapper.git'\n",
48-
"\n",
49-
"from socialmapper import (\n",
50-
" get_poi,\n",
51-
" import_poi_csv,\n",
52-
" create_isochrone,\n",
53-
" get_census_blocks,\n",
54-
" get_census_data,\n",
55-
" create_map,\n",
56-
")\n",
57-
"from socialmapper.poi_categorization import POI_CATEGORY_MAPPING\n",
58-
"\n",
59-
"import matplotlib.pyplot as plt\n",
60-
"import pandas as pd\n",
61-
"from IPython.display import Image, display"
62-
]
45+
"source": "# Uncomment to install on Google Colab:\n# !pip install 'socialmapper @ git+https://github.com/mihiarc/socialmapper.git'\n\nfrom socialmapper import (\n get_poi,\n import_poi_csv,\n create_isochrone,\n get_census_blocks,\n get_census_data,\n create_map,\n)\nfrom socialmapper.poi_categorization import POI_CATEGORY_MAPPING\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport geopandas as gpd\nimport contextily as cx\nfrom IPython.display import Image, display"
6346
},
6447
{
6548
"cell_type": "markdown",
@@ -432,46 +415,7 @@
432415
"id": "cell-22",
433416
"metadata": {},
434417
"outputs": [],
435-
"source": [
436-
"fig, ax = plt.subplots(figsize=(9, 9))\n",
437-
"\n",
438-
"# Assign a color to each category\n",
439-
"unique_categories = df_all[\"category\"].unique()\n",
440-
"color_map = {cat: plt.cm.Set2(i / max(len(unique_categories) - 1, 1)) for i, cat in enumerate(unique_categories)}\n",
441-
"\n",
442-
"for category in unique_categories:\n",
443-
" subset = df_all[df_all[\"category\"] == category]\n",
444-
" ax.scatter(\n",
445-
" subset[\"lon\"],\n",
446-
" subset[\"lat\"],\n",
447-
" c=[color_map[category]],\n",
448-
" label=f\"{category} ({len(subset)})\",\n",
449-
" s=40,\n",
450-
" alpha=0.75,\n",
451-
" edgecolors=\"white\",\n",
452-
" linewidths=0.5,\n",
453-
" )\n",
454-
"\n",
455-
"# Mark the origin\n",
456-
"ax.scatter(\n",
457-
" -122.3321, 47.6062,\n",
458-
" c=\"red\", marker=\"*\", s=200, zorder=5,\n",
459-
" label=\"Origin (Seattle center)\",\n",
460-
" edgecolors=\"darkred\", linewidths=0.8,\n",
461-
")\n",
462-
"\n",
463-
"ax.set_xlabel(\"Longitude\", fontsize=11)\n",
464-
"ax.set_ylabel(\"Latitude\", fontsize=11)\n",
465-
"ax.set_title(\n",
466-
" \"POI Locations by Category near Seattle, WA\",\n",
467-
" fontsize=13,\n",
468-
" fontweight=\"bold\",\n",
469-
")\n",
470-
"ax.legend(loc=\"upper left\", fontsize=8, framealpha=0.9)\n",
471-
"ax.set_aspect(\"equal\")\n",
472-
"plt.tight_layout()\n",
473-
"plt.show()"
474-
]
418+
"source": "fig, ax = plt.subplots(figsize=(9, 9))\n\n# Assign a color to each category\nunique_categories = df_all[\"category\"].unique()\ncolor_map = {cat: plt.cm.Set2(i / max(len(unique_categories) - 1, 1)) for i, cat in enumerate(unique_categories)}\n\nfor category in unique_categories:\n subset = df_all[df_all[\"category\"] == category]\n ax.scatter(\n subset[\"lon\"],\n subset[\"lat\"],\n c=[color_map[category]],\n label=f\"{category} ({len(subset)})\",\n s=40,\n alpha=0.75,\n edgecolors=\"white\",\n linewidths=0.5,\n zorder=5,\n )\n\n# Mark the origin\nax.scatter(\n -122.3321, 47.6062,\n c=\"red\", marker=\"*\", s=200, zorder=6,\n label=\"Origin (Seattle center)\",\n edgecolors=\"darkred\", linewidths=0.8,\n)\n\n# Add road basemap for geographic context\ncx.add_basemap(ax, crs=\"EPSG:4326\", source=cx.providers.CartoDB.Positron)\n\nax.set_xlabel(\"Longitude\", fontsize=11)\nax.set_ylabel(\"Latitude\", fontsize=11)\nax.set_title(\n \"POI Locations by Category near Seattle, WA\",\n fontsize=13,\n fontweight=\"bold\",\n)\nax.legend(loc=\"upper left\", fontsize=8, framealpha=0.9)\nplt.tight_layout()\nplt.show()"
475419
},
476420
{
477421
"cell_type": "markdown",

0 commit comments

Comments
 (0)