Where are America’s “super zips”?

Maps & geospatial
Charts
An ipyleaflet heatmap of 10,000 US zip codes linked to four plotly density plots by the map’s viewport — pan and the plots redraw for the zips in view; zoom in and the heatmap swaps to clickable markers.

Charles Murray coined “super zips” for the country’s most prosperous, highly educated zip codes, and this app — a port of the classic Shiny demo — makes them explorable. An ipyleaflet heatmap colors 10,000 sampled zips by score, and four plotly density plots in the sidebar compare every zip in the country (black) against just the zips inside the map’s current viewport (green). Pan from the national view to the Northeast corridor and the green curves visibly shift right: that’s what a superzip cluster looks like. Zoom in past 200 zips and the heatmap swaps to viridis-colored circle markers; click one and its score, college rate, income, and population appear in a card on the map, with a dashed line marking its value on every density plot. A second tab lists the in-view zips as a data grid — pick a row and a locator map drops a marker on it.

Where are America’s “super zips”?

Run it

git clone --depth 1 https://github.com/posit-dev/py-shinywidgets
cd py-shinywidgets/website/gallery/showcases/superzip
uv run --with faicons --with scipy shiny run app.py

data/zips.parquet is committed (prepared by prep_data.py from the full 31,713-zip CSV, which the app only ever sampled 10,000 rows from anyway). There is no Shinylive link: ipyleaflet tiles and a server-side reactive graph don’t belong in a browser tab.

Source: app.py

Try this

  • Pan across the country. The four density plots redraw for the zips in view: the Northeast corridor shifts every green curve right of the national curve, while rural regions shift them left. A 300 ms debounce on the viewport calc means the plots redraw once your gesture settles, not on every intermediate frame.
  • Zoom into a metro. Once fewer than 200 zips are in view, the heatmap swaps to circle markers colored by the selected variable. Zoom back out and the heatmap returns.
  • Click a marker. A card on the map names the zip and its metrics, and a dashed line marks its value on all four density plots — an instant read on how that zip compares to its neighbors and to the country.
  • Switch the variable. The selector recolors the heatmap (or markers) between overall score, college education, income, and population.
  • Open the Data explorer tab. The grid lists exactly the zips in the map’s viewport and updates as you pan; click a row to drop a marker on the locator map below it.

Techniques demonstrated

  • Viewport as reactive input. map.bounds() (an ipyleaflet accessor) feeds a debounced reactive.calc that filters the dataset to the visible zips; the density plots, the data grid, and the intro summary all read that one calc. Because the map reports bogus bounds while its tab is hidden, bounds are only tracked while the “Interactive map” tab is showing.
  • Layer swap instead of re-render. The heatmap/markers toggle mutates map.widget.layers in a reactive.effect — remove the old data layers, add the new ones — so the map widget (zoom, position, basemap) is never re-created. The effect only reads zips_in_bounds() in marker mode, so panning the zoomed-out heatmap doesn’t churn it.
  • Widget events as reactive values. Each CircleMarker’s on_click sets a reactive.value; a Shiny-rendered overlay card and the plotly dashed lines both read it. (The original demo used an ipywidgets popup, which doesn’t work with ipywidgets 8; the Shiny-side card replaces it.)

Verification

Driven end-to-end with Playwright in headless Chromium: the national view renders the heatmap (48 basemap tiles, one overlay canvas) and all four density plots; panning and zooming to the Kansas City metro swaps the heatmap for 99 markers and shifts the green “in bounds” curves off the black “overall” curves; clicking a marker opens the overlay (“Blue Springs, MO 64015 · Score 73.0 · 28.7% college educated · $82k median income”) and draws dashed lines at its values on every density plot; “Clear selection” removes both. The Data explorer tab counts the in-view zips, filters the grid, and drops a marker on its locator map when a row is clicked. Zooming to a spot with a single zip leaves the plots empty instead of erroring (a KDE needs two points). No page errors or server-log errors throughout. img/hero.webp is the national view; img/after_zoom.webp is the Kansas City metro with a zip selected.