Skip to content

Instantly share code, notes, and snippets.

@knaaptime
Last active January 7, 2025 19:46
Show Gist options
  • Select an option

  • Save knaaptime/cc6863d99a4a08a9825191dc27b3a5f1 to your computer and use it in GitHub Desktop.

Select an option

Save knaaptime/cc6863d99a4a08a9825191dc27b3a5f1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "031750c4-cc25-4db6-b112-ca0c9e2aa2a5",
"metadata": {},
"outputs": [],
"source": [
"import geodatasets\n",
"import geopandas as gpd\n",
"from ipywidgets import fixed, interact\n",
"from lonboard import viz\n",
"from mapclassify import classify\n",
"from mapclassify._classify_API import _classifiers\n",
"from mapclassify.util import get_color_array\n",
"from matplotlib import colormaps"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c00aba3d-c8a1-4a2b-8196-7bb0117d1d5c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Author: eli knaap\n",
"\n",
"ipywidgets : 8.1.5\n",
"geopandas : 1.0.1\n",
"mapclassify: 2.8.2.dev2+ga421b4b.d20250107\n",
"geodatasets: 2024.8.0\n",
"lonboard : 0.10.3\n",
"\n"
]
}
],
"source": [
"%load_ext watermark\n",
"%watermark -a 'eli knaap' -iv"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a76ef991-18f8-4737-87a4-019056e7ffa4",
"metadata": {},
"outputs": [],
"source": [
"gdf = gpd.read_file(geodatasets.get_path(\"geoda.milwaukee1\"))\n",
"gdf = gdf.to_crs(4326)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "0a25a854-fb8b-4026-872a-096aa8dcc9b4",
"metadata": {},
"outputs": [],
"source": [
"def choro(gdf, column, classifier, cmap, k, layer):\n",
" layer.get_fill_color = get_color_array(values=gdf[column].values, scheme=classifier, k=k, cmap=cmap)\n",
" return classify(gdf[column].values, scheme=classifier, k=k).plot_legendgram(cmap=cmap, inset=False, vlines=True, bins=60)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8e0d3d91-c07d-452f-953c-27a903fccab1",
"metadata": {},
"outputs": [],
"source": [
"m = viz(gdf)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "16359f7a-4ddd-4fab-a79c-196f04545ba8",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a69010139f994359afcb9d6e55ec85c7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(Dropdown(description='column', options=('TOT_POP', 'POP_16', 'POP_65', 'WHITE_', 'BLACK_…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"<function __main__.choro(gdf, column, classifier, cmap, k, layer)>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"interact(\n",
" choro,\n",
" gdf=fixed(gdf),\n",
" column=gdf.columns[2:].values, # ignore first two string cols\n",
" classifier=list(_classifiers.keys()),\n",
" k=list(range(3, 16)),\n",
" cmap=list(colormaps.keys()),\n",
" layer=fixed(m.layers[0]),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "787d9689-f94b-4242-91e4-2899875063e7",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "40a27d6edea14242b6628ba0489cbc46",
"version_major": 2,
"version_minor": 1
},
"text/plain": [
"Map(basemap_style=<CartoBasemap.DarkMatter: 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json'…"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a4f2da23-e74b-4d32-ba4b-bec362af740f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:urban_analysis]",
"language": "python",
"name": "conda-env-urban_analysis-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment