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\n from socialmapper import create_isochrone, get_census_blocks\n\n import matplotlib.pyplot as plt\n import pandas as pd\n import geopandas as gpd\n import contextily as cx"
8072 },
8173 {
8274 "cell_type" : " markdown" ,
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" ,
559511 },
560512 "nbformat" : 4 ,
561513 "nbformat_minor" : 4
562- }
514+ }
0 commit comments