{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Choropleth\n", "\n", "`mapclassify` is intended to be used with visualizaiton packages to handle the actual rendering of the choropleth maps defined on its classifiers. In this notebook, we explore some examples of how this is done. The notebook also includes an example that combines `mapclassify` with [ipywidgets](https://ipywidgets.readthedocs.io/en/latest/) to allow for the interactive exploration of the choice of:\n", "\n", "- classification method\n", "- number of classes\n", "- colormap" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2022-11-05T01:00:43.535547Z", "start_time": "2022-11-05T01:00:40.391594Z" } }, "outputs": [ { "data": { "text/plain": [ "'2.4.2+78.gc62d2d7.dirty'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import libpysal\n", "import geopandas\n", "import mapclassify\n", "import matplotlib.pyplot as plt\n", "\n", "mapclassify.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The example in this notebook uses data on sudden death infant syndrome for counties in North Carolina. It is a built-in dataset available through `libpysal`. We use `libpysal` to obtain the path to the shapefile and then use `geopandas` to create a geodataframe from the shapefile:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2022-11-05T01:00:43.545340Z", "start_time": "2022-11-05T01:00:43.539863Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sids2\n", "=====\n", "\n", "North Carolina county SIDS death counts and rates\n", "-------------------------------------------------\n", "\n", "* sids2.dbf: attribute data. (k=18)\n", "* sids2.html: metadata.\n", "* sids2.shp: Polygon shapefile. (n=100)\n", "* sids2.shx: spatial index.\n", "* sids2.gal: spatial weights in GAL format.\n", "\n", "Source: Cressie, Noel (1993). Statistics for Spatial Data. New York, Wiley, pp. 386-389. Rates computed.\n", "Updated URL: https://geodacenter.github.io/data-and-lab/sids2/\n", "\n" ] } ], "source": [ "libpysal.examples.explain(\"sids2\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2022-11-05T01:00:43.733714Z", "start_time": "2022-11-05T01:00:43.549872Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " | AREA | \n", "PERIMETER | \n", "CNTY_ | \n", "CNTY_ID | \n", "NAME | \n", "FIPS | \n", "FIPSNO | \n", "CRESS_ID | \n", "BIR74 | \n", "SID74 | \n", "NWBIR74 | \n", "BIR79 | \n", "SID79 | \n", "NWBIR79 | \n", "SIDR74 | \n", "SIDR79 | \n", "NWR74 | \n", "NWR79 | \n", "geometry | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "0.114 | \n", "1.442 | \n", "1825 | \n", "1825 | \n", "Ashe | \n", "37009 | \n", "37009 | \n", "5 | \n", "1091.0 | \n", "1.0 | \n", "10.0 | \n", "1364.0 | \n", "0.0 | \n", "19.0 | \n", "0.916590 | \n", "0.000000 | \n", "9.165903 | \n", "13.929619 | \n", "POLYGON ((-81.47276 36.23436, -81.54084 36.272... | \n", "
1 | \n", "0.061 | \n", "1.231 | \n", "1827 | \n", "1827 | \n", "Alleghany | \n", "37005 | \n", "37005 | \n", "3 | \n", "487.0 | \n", "0.0 | \n", "10.0 | \n", "542.0 | \n", "3.0 | \n", "12.0 | \n", "0.000000 | \n", "5.535055 | \n", "20.533881 | \n", "22.140221 | \n", "POLYGON ((-81.23989 36.36536, -81.24069 36.379... | \n", "
2 | \n", "0.143 | \n", "1.630 | \n", "1828 | \n", "1828 | \n", "Surry | \n", "37171 | \n", "37171 | \n", "86 | \n", "3188.0 | \n", "5.0 | \n", "208.0 | \n", "3616.0 | \n", "6.0 | \n", "260.0 | \n", "1.568381 | \n", "1.659292 | \n", "65.244668 | \n", "71.902655 | \n", "POLYGON ((-80.45634 36.24256, -80.47639 36.254... | \n", "
3 | \n", "0.070 | \n", "2.968 | \n", "1831 | \n", "1831 | \n", "Currituck | \n", "37053 | \n", "37053 | \n", "27 | \n", "508.0 | \n", "1.0 | \n", "123.0 | \n", "830.0 | \n", "2.0 | \n", "145.0 | \n", "1.968504 | \n", "2.409639 | \n", "242.125984 | \n", "174.698795 | \n", "MULTIPOLYGON (((-76.00897 36.31960, -76.01735 ... | \n", "
4 | \n", "0.153 | \n", "2.206 | \n", "1832 | \n", "1832 | \n", "Northampton | \n", "37131 | \n", "37131 | \n", "66 | \n", "1421.0 | \n", "9.0 | \n", "1066.0 | \n", "1606.0 | \n", "3.0 | \n", "1197.0 | \n", "6.333568 | \n", "1.867995 | \n", "750.175932 | \n", "745.330012 | \n", "POLYGON ((-77.21767 36.24098, -77.23461 36.214... | \n", "