{ "cells": [ { "cell_type": "markdown", "id": "header", "metadata": {}, "source": [ "# Flow Brush Map API - Interactive Migration Visualization\n", "\n", "This notebook demonstrates the new `flow_brushmap()` API in pytidycensus, which provides a simple, high-level interface for creating interactive migration flow visualizations.\n", "\n", "The `flow_brushmap()` function handles all the complexity of:\n", "- Processing migration flow data\n", "- Converting to GeoArrow format\n", "- Creating arc, source, and target layers\n", "- Configuring the BrushingExtension\n", "- Building an interactive lonboard map\n", "\n", "All you need is data from `get_flows()` with `geometry=True`!" ] }, { "cell_type": "markdown", "id": "installation", "metadata": {}, "source": [ "## Installation\n", "\n", "To use the mapping functions, install pytidycensus with the map extra:\n", "\n", "```bash\n", "pip install pytidycensus[map]\n", "```\n", "\n", "This installs:\n", "- lonboard >= 0.12.1\n", "- pyarrow >= 19.0.0\n", "- folium >= 0.20.0\n", "- contextily >= 1.6.2\n", "- branca >= 0.8.2" ] }, { "cell_type": "markdown", "id": "imports_header", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 1, "id": "imports", "metadata": {}, "outputs": [], "source": [ "import pytidycensus as tc\n", "from pytidycensus.mapping import flow_brushmap, quick_flow_map" ] }, { "cell_type": "markdown", "id": "basic_usage_header", "metadata": {}, "source": [ "## Basic Usage\n", "\n", "The simplest way to create a flow brush map is to use `quick_flow_map()`, which fetches data and creates the map in one call:" ] }, { "cell_type": "code", "execution_count": null, "id": "quick_map", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Fetching 2018 county-level migration flows for PA...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/mmann1123/Documents/github/pytidycensus/pytidycensus/flows.py:654: UserWarning: Could not find centroids for 8 GEOIDs: ['09009', '09003', '09005', '09001', '09013'].... These flows will not have geometry data.\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Processing 13778 flow records...\n", "Created 2076 arcs, 2503 sources, and 67 targets\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9f2134fd83a34fc48f9db9be1db36c47", "version_major": 2, "version_minor": 1 }, "text/plain": [ "Map(custom_attribution='', layers=(ScatterplotLayer(brushing_radius=50000.0, extensions=(BrushingExtension(),)…" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Quick one-liner: fetch data and create map\n", "map_ = quick_flow_map(\n", " state=\"PA\", \n", " year=2018,\n", " flow_threshold=50, # Show flows >= 50 people\n", " brushing_radius=50000 # 50km brushing radius\n", ")\n", "map_" ] }, { "cell_type": "markdown", "id": "standard_usage_header", "metadata": {}, "source": [ "## Standard Usage: Two-Step Approach\n", "\n", "For more control, fetch the data first, then create the map:" ] }, { "cell_type": "code", "execution_count": 3, "id": "fetch_data", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Retrieved 36641 flow records\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/home/mmann1123/Documents/github/pytidycensus/pytidycensus/flows.py:654: UserWarning: Could not find centroids for 97 GEOIDs: ['0900108980', '0900715350', '0900308490', '0900761800', '0900387000'].... These flows will not have geometry data.\n", " warnings.warn(\n" ] }, { "data": { "text/html": [ "
| \n", " | GEOID1\n", " | GEOID2\n", " | FULL1_NAME\n", " | FULL2_NAME\n", " | MOVEDIN\n", " | MOVEDIN_M\n", " | MOVEDOUT\n", " | MOVEDOUT_M\n", " | MOVEDNET\n", " | MOVEDNET_M\n", " | centroid1\n", " | centroid2\n", " | 
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0\n", " | 48001\n", " | None\n", " | Anderson County, Texas\n", " | Africa\n", " | 38\n", " | 52.0\n", " | NaN\n", " | NaN\n", " | NaN\n", " | NaN\n", " | POINT (-95.65236 31.81326)\n", " | NaN\n", " | 
| 1\n", " | 48001\n", " | None\n", " | Anderson County, Texas\n", " | Asia\n", " | 4\n", " | 6.0\n", " | NaN\n", " | NaN\n", " | NaN\n", " | NaN\n", " | POINT (-95.65236 31.81326)\n", " | NaN\n", " | 
| 2\n", " | 48001\n", " | None\n", " | Anderson County, Texas\n", " | Central America\n", " | 2\n", " | 3.0\n", " | NaN\n", " | NaN\n", " | NaN\n", " | NaN\n", " | POINT (-95.65236 31.81326)\n", " | NaN\n", " | 
| 3\n", " | 48001\n", " | 01089\n", " | Anderson County, Texas\n", " | Madison County, Alabama\n", " | 13\n", " | 20.0\n", " | 0.0\n", " | 28.0\n", " | 13.0\n", " | 20.0\n", " | POINT (-95.65236 31.81326)\n", " | POINT (-86.55022579567611 34.762959383197796)\n", " | 
| 4\n", " | 48001\n", " | 02016\n", " | Anderson County, Texas\n", " | Aleutians West Census Area, Alaska\n", " | 0\n", " | 31.0\n", " | 7.0\n", " | 9.0\n", " | -7.0\n", " | 9.0\n", " | POINT (-95.65236 31.81326)\n", " | POINT (-173.77316055538125 52.983281670565866)\n", " |