diff --git a/notebooks/density-splits.ipynb b/notebooks/density-splits.ipynb new file mode 100644 index 00000000..5c7f1a1a --- /dev/null +++ b/notebooks/density-splits.ipynb @@ -0,0 +1,4154 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "a00658f0", + "metadata": {}, + "outputs": [], + "source": [ + "import os; os.chdir(\"..\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "cfa5b130", + "metadata": {}, + "outputs": [], + "source": [ + "%load_ext autoreload\n", + "%autoreload 2\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "code", + "execution_count": 196, + "id": "b2d6c4cb", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "\n", + "import tqdm.auto as tqdm\n", + "import pandas as pd\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "from urbanstats.unrelated_projects.density_and_partisanship import *" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "1cdc0009", + "metadata": {}, + "outputs": [], + "source": [ + "n = 3\n", + "bins, data = load_data(n=n)" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "id": "1ff3dbe5", + "metadata": {}, + "outputs": [], + "source": [ + "pop = np.array(data.population)\n", + "dens = np.array(data.density)" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "520e2fab", + "metadata": {}, + "outputs": [], + "source": [ + "dgt = np.array([\n", + " np.array(data[k])\n", + " for k in partisanship_columns\n", + "]).T" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "bdd777fb", + "metadata": {}, + "outputs": [], + "source": [ + "states = sorted(set(data[\"state\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "4ba30ba6", + "metadata": {}, + "outputs": [], + "source": [ + "usa_whole = compute_by_bin(bins, dgt, dens)" + ] + }, + { + "cell_type": "code", + "execution_count": 194, + "id": "56fca443", + "metadata": {}, + "outputs": [], + "source": [ + "state_overall = np.array(data.groupby(\"state\").sum()[partisanship_columns].loc[states])\n", + "state_margins = [margin(*x) for x in state_overall]" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "id": "527318c9", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "d935af6b9f86425e8086c2e511e5f314", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + " 0%| | 0/51 [00:00
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "plot_density_categories(states, by_state)" + ] + }, + { + "cell_type": "code", + "execution_count": 202, + "id": "e7102acf", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.plotly.v1+json": { + "config": { + "plotlyServerURL": "https://plot.ly" + }, + "data": [ + { + "colorbar": { + "title": { + "text": "Margin" + } + }, + "colorscale": [ + [ + 0, + "rgb(103,0,31)" + ], + [ + 0.1, + "rgb(178,24,43)" + ], + [ + 0.2, + "rgb(214,96,77)" + ], + [ + 0.3, + "rgb(244,165,130)" + ], + [ + 0.4, + "rgb(253,219,199)" + ], + [ + 0.5, + "rgb(247,247,247)" + ], + [ + 0.6, + "rgb(209,229,240)" + ], + [ + 0.7, + "rgb(146,197,222)" + ], + [ + 0.8, + "rgb(67,147,195)" + ], + [ + 0.9, + "rgb(33,102,172)" + ], + [ + 1, + "rgb(5,48,97)" + ] + ], + "geo": "geo", + "locationmode": "USA-states", + "locations": [ + "AL", + "AK", + "AZ", + "AR", + "CA", + "CO", + "CT", + "DE", + "DC", + "FL", + "GA", + "HI", + "ID", + "IL", + "IN", + "IA", + "KS", + "KY", + "LA", + "ME", + "MD", + "MA", + "MI", + "MN", + "MS", + "MO", + "MT", + "NE", + "NV", + "NH", + "NJ", + "NM", + "NY", + "NC", + "ND", + "OH", + "OK", + "OR", + "PA", + "RI", + "SC", + "SD", + "TN", + "TX", + "UT", + "VT", + "VA", + "WA", + "WV", + "WI", + "WY" + ], + "type": "choropleth", + "z": [ + -0.16026520697885271, + -0.10674004982383309, + -0.1613913746875833, + -0.14601007356186924, + -0.317070344778803, + -0.27410103604073355, + -0.10333441627866462, + -0.25405509161151535, + -0.02646684388820586, + -0.25390054696933523, + -0.29891817156916684, + 0.04818908016034634, + -0.18525238477575662, + -0.4545766178304742, + -0.24033062616573483, + -0.2028578867506601, + -0.27930555896127096, + -0.23178921565014338, + -0.2504031287239385, + -0.06690259048110367, + -0.3949542453426862, + -0.12932344647218655, + -0.24813509556321095, + -0.3083651334967392, + -0.09240757215570902, + -0.3360879484389936, + -0.14509688829684222, + -0.3303667861022227, + -0.37603318958537846, + -0.046582956834485295, + -0.2453875114221869, + -0.08116831238673472, + -0.32085325277499044, + -0.21955303142290755, + -0.15751030558357793, + -0.2980367652458458, + -0.2034037522120477, + -0.3517483553581194, + -0.3043964486472401, + -0.12381819757495484, + -0.08855812705472549, + -0.1047356777123703, + -0.23401124916953028, + -0.3919978446160143, + -0.2674569566730856, + -0.047988661688718515, + -0.34566498859632316, + -0.27244706062971474, + -0.08616466334627843, + -0.21921140164083944, + -0.11520857744258983 + ], + "zmax": 1, + "zmin": -1 + }, + { + "colorbar": { + "title": { + "text": "Margin" + } + }, + "colorscale": [ + [ + 0, + "rgb(103,0,31)" + ], + [ + 0.1, + "rgb(178,24,43)" + ], + [ + 0.2, + "rgb(214,96,77)" + ], + [ + 0.3, + "rgb(244,165,130)" + ], + [ + 0.4, + "rgb(253,219,199)" + ], + [ + 0.5, + "rgb(247,247,247)" + ], + [ + 0.6, + "rgb(209,229,240)" + ], + [ + 0.7, + "rgb(146,197,222)" + ], + [ + 0.8, + "rgb(67,147,195)" + ], + [ + 0.9, + "rgb(33,102,172)" + ], + [ + 1, + "rgb(5,48,97)" + ] + ], + "geo": "geo2", + "locationmode": "USA-states", + "locations": [ + "AL", + "AK", + "AZ", + "AR", + "CA", + "CO", + "CT", + "DE", + "DC", + "FL", + "GA", + "HI", + "ID", + "IL", + "IN", + "IA", + "KS", + "KY", + "LA", + "ME", + "MD", + "MA", + "MI", + "MN", + "MS", + "MO", + "MT", + "NE", + "NV", + "NH", + "NJ", + "NM", + "NY", + "NC", + "ND", + "OH", + "OK", + "OR", + "PA", + "RI", + "SC", + "SD", + "TN", + "TX", + "UT", + "VT", + "VA", + "WA", + "WV", + "WI", + "WY" + ], + "type": "choropleth", + "z": [ + 0.33039195411109384, + 0.0950537014687904, + -0.05332036434153745, + 0.28478374095234793, + -0.13821294307740042, + -0.05316470122750164, + -0.005789226664373259, + 0.09302803358420378, + -0.05004665652274154, + -0.007095306876575842, + 0.22533465883477552, + -0.0009478404095479842, + 0.06602131245838808, + -0.06306634018695462, + 0.1452722690152269, + 0.13771868201825757, + 0.14763188745446204, + 0.21088600538166544, + 0.17741422314156655, + 0.19090921950385276, + 0.017334100756237092, + -0.05654452594290976, + 0.10073649100543765, + 0.06315783529857313, + 0.29749839921373844, + 0.1596100238856349, + 0.15742902961606597, + 0.08193156184147199, + -0.10690355565757358, + 0.07673232608846961, + -0.08777567829672663, + 0.007860005541283285, + -0.1478158293521357, + 0.2577798406063067, + 0.06637537755642048, + 0.07943516119178436, + 0.16142363541015983, + -0.027638968620763565, + 0.012613878283702792, + -0.0573865304496424, + 0.12086004141633912, + 0.10108178056821829, + 0.2654621711942743, + 0.035220661443836995, + -0.10157139992735514, + 0.12377514625311586, + 0.09506100162274879, + -0.04017370059960221, + 0.245200082991175, + 0.0829762532438062, + 0.12591842351660976 + ], + "zmax": 1, + "zmin": -1 + }, + { + "colorbar": { + "title": { + "text": "Margin" + } + }, + "colorscale": [ + [ + 0, + "rgb(103,0,31)" + ], + [ + 0.1, + "rgb(178,24,43)" + ], + [ + 0.2, + "rgb(214,96,77)" + ], + [ + 0.3, + "rgb(244,165,130)" + ], + [ + 0.4, + "rgb(253,219,199)" + ], + [ + 0.5, + "rgb(247,247,247)" + ], + [ + 0.6, + "rgb(209,229,240)" + ], + [ + 0.7, + "rgb(146,197,222)" + ], + [ + 0.8, + "rgb(67,147,195)" + ], + [ + 0.9, + "rgb(33,102,172)" + ], + [ + 1, + "rgb(5,48,97)" + ] + ], + "geo": "geo3", + "locationmode": "USA-states", + "locations": [ + "AL", + "AK", + "AZ", + "AR", + "CA", + "CO", + "CT", + "DE", + "DC", + "FL", + "GA", + "HI", + "ID", + "IL", + "IN", + "IA", + "KS", + "KY", + "LA", + "ME", + "MD", + "MA", + "MI", + "MN", + "MS", + "MO", + "MT", + "NE", + "NV", + "NH", + "NJ", + "NM", + "NY", + "NC", + "ND", + "OH", + "OK", + "OR", + "PA", + "RI", + "SC", + "SD", + "TN", + "TX", + "UT", + "VT", + "VA", + "WA", + "WV", + "WI", + "WY" + ], + "type": "choropleth", + "z": [ + 0.5990858620384291, + 0.20450803627932662, + 0.14154426760314912, + 0.5420696964336742, + 0.09060638149406186, + 0.19689618727129507, + 0.24621413771744602, + 0.37709894884311745, + 0.0011186069433007928, + 0.22693305611091713, + 0.5379201750620235, + -0.018588254859381537, + 0.2538389703298307, + 0.3250242593091498, + 0.5105943570297984, + 0.3696420363697185, + 0.34924082539558, + 0.5807552467113681, + 0.47687276288033453, + 0.3897620446104066, + 0.2688901736674381, + 0.1844374185107323, + 0.3587848305900924, + 0.4818448915008898, + 0.3394823827813771, + 0.57590271641178, + 0.4194705200288783, + 0.37492782630058286, + 0.131703711894872, + 0.21318876807922202, + 0.2107643666515003, + 0.13479369923510948, + 0.2031747290027295, + 0.5488606727274852, + 0.3047913456488245, + 0.4650082999242621, + 0.38172454777872256, + 0.26608659802178236, + 0.4441166234889817, + 0.17700359870575993, + 0.44317111390836805, + 0.2516608979582329, + 0.6643146219162216, + 0.27690329844184464, + 0.21096632788311925, + 0.3613778157566297, + 0.35868995721871133, + 0.25499339489641243, + 0.5987212245906539, + 0.4349807529210164, + 0.3050694559663879 + ], + "zmax": 1, + "zmin": -1 + } + ], + "layout": { + "annotations": [ + { + "font": { + "size": 16 + }, + "showarrow": false, + "text": "Among Least Dense: Margin relative to state", + "x": 0.5, + "xanchor": "center", + "xref": "paper", + "y": 1, + "yanchor": "bottom", + "yref": "paper" + }, + { + "font": { + "size": 16 + }, + "showarrow": false, + "text": "Among Middle Dense: Margin relative to state", + "x": 0.5, + "xanchor": "center", + "xref": "paper", + "y": 0.6499999999999999, + "yanchor": "bottom", + "yref": "paper" + }, + { + "font": { + "size": 16 + }, + "showarrow": false, + "text": "Among Most Dense: Margin relative to state", + "x": 0.5, + "xanchor": "center", + "xref": "paper", + "y": 0.3, + "yanchor": "bottom", + "yref": "paper" + } + ], + "autosize": false, + "geo": { + "domain": { + "x": [ + 0, + 1 + ], + "y": [ + 0.7, + 1 + ] + }, + "lakecolor": "rgb(255, 255, 255)", + "projection": { + "type": "albers usa" + }, + "showlakes": true + }, + "geo2": { + "domain": { + "x": [ + 0, + 1 + ], + "y": [ + 0.35, + 0.6499999999999999 + ] + }, + "lakecolor": "rgb(255, 255, 255)", + "projection": { + "type": "albers usa" + }, + "showlakes": true + }, + "geo3": { + "domain": { + "x": [ + 0, + 1 + ], + "y": [ + 0, + 0.3 + ] + }, + "lakecolor": "rgb(255, 255, 255)", + "projection": { + "type": "albers usa" + }, + "showlakes": true + }, + "height": 960, + "template": { + "data": { + "bar": [ + { + "error_x": { + "color": "#2a3f5f" + }, + "error_y": { + "color": "#2a3f5f" + }, + "marker": { + "line": { + "color": "#E5ECF6", + "width": 0.5 + }, + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "bar" + } + ], + "barpolar": [ + { + "marker": { + "line": { + "color": "#E5ECF6", + "width": 0.5 + }, + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "barpolar" + } + ], + "carpet": [ + { + "aaxis": { + "endlinecolor": "#2a3f5f", + "gridcolor": "white", + "linecolor": "white", + "minorgridcolor": "white", + "startlinecolor": "#2a3f5f" + }, + "baxis": { + "endlinecolor": "#2a3f5f", + "gridcolor": "white", + "linecolor": "white", + "minorgridcolor": "white", + "startlinecolor": "#2a3f5f" + }, + "type": "carpet" + } + ], + "choropleth": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "choropleth" + } + ], + "contour": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "contour" + } + ], + "contourcarpet": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "contourcarpet" + } + ], + "heatmap": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "heatmap" + } + ], + "heatmapgl": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "heatmapgl" + } + ], + "histogram": [ + { + "marker": { + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "histogram" + } + ], + "histogram2d": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "histogram2d" + } + ], + "histogram2dcontour": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "histogram2dcontour" + } + ], + "mesh3d": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "mesh3d" + } + ], + "parcoords": [ + { + "line": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "parcoords" + } + ], + "pie": [ + { + "automargin": true, + "type": "pie" + } + ], + "scatter": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatter" + } + ], + "scatter3d": [ + { + "line": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatter3d" + } + ], + "scattercarpet": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattercarpet" + } + ], + "scattergeo": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattergeo" + } + ], + "scattergl": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattergl" + } + ], + "scattermapbox": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattermapbox" + } + ], + "scatterpolar": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterpolar" + } + ], + "scatterpolargl": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterpolargl" + } + ], + "scatterternary": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterternary" + } + ], + "surface": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "surface" + } + ], + "table": [ + { + "cells": { + "fill": { + "color": "#EBF0F8" + }, + "line": { + "color": "white" + } + }, + "header": { + "fill": { + "color": "#C8D4E3" + }, + "line": { + "color": "white" + } + }, + "type": "table" + } + ] + }, + "layout": { + "annotationdefaults": { + "arrowcolor": "#2a3f5f", + "arrowhead": 0, + "arrowwidth": 1 + }, + "autotypenumbers": "strict", + "coloraxis": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "colorscale": { + "diverging": [ + [ + 0, + "#8e0152" + ], + [ + 0.1, + "#c51b7d" + ], + [ + 0.2, + "#de77ae" + ], + [ + 0.3, + "#f1b6da" + ], + [ + 0.4, + "#fde0ef" + ], + [ + 0.5, + "#f7f7f7" + ], + [ + 0.6, + "#e6f5d0" + ], + [ + 0.7, + "#b8e186" + ], + [ + 0.8, + "#7fbc41" + ], + [ + 0.9, + "#4d9221" + ], + [ + 1, + "#276419" + ] + ], + "sequential": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "sequentialminus": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ] + }, + "colorway": [ + "#636efa", + "#EF553B", + "#00cc96", + "#ab63fa", + "#FFA15A", + "#19d3f3", + "#FF6692", + "#B6E880", + "#FF97FF", + "#FECB52" + ], + "font": { + "color": "#2a3f5f" + }, + "geo": { + "bgcolor": "white", + "lakecolor": "white", + "landcolor": "#E5ECF6", + "showlakes": true, + "showland": true, + "subunitcolor": "white" + }, + "hoverlabel": { + "align": "left" + }, + "hovermode": "closest", + "mapbox": { + "style": "light" + }, + "paper_bgcolor": "white", + "plot_bgcolor": "#E5ECF6", + "polar": { + "angularaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "bgcolor": "#E5ECF6", + "radialaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + } + }, + "scene": { + "xaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + }, + "yaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + }, + "zaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + } + }, + "shapedefaults": { + "line": { + "color": "#2a3f5f" + } + }, + "ternary": { + "aaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "baxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "bgcolor": "#E5ECF6", + "caxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + } + }, + "title": { + "x": 0.05 + }, + "xaxis": { + "automargin": true, + "gridcolor": "white", + "linecolor": "white", + "ticks": "", + "title": { + "standoff": 15 + }, + "zerolinecolor": "white", + "zerolinewidth": 2 + }, + "yaxis": { + "automargin": true, + "gridcolor": "white", + "linecolor": "white", + "ticks": "", + "title": { + "standoff": 15 + }, + "zerolinecolor": "white", + "zerolinewidth": 2 + } + } + }, + "width": 800 + } + }, + "text/html": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "plot_density_categories(states, by_state - np.array(state_margins)[:,None], \" relative to state\")" + ] + }, + { + "cell_type": "code", + "execution_count": 207, + "id": "bdc82f91", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.plotly.v1+json": { + "config": { + "plotlyServerURL": "https://plot.ly" + }, + "data": [ + { + "colorbar": { + "title": { + "text": "Margin" + } + }, + "colorscale": [ + [ + 0, + "rgb(103,0,31)" + ], + [ + 0.1, + "rgb(178,24,43)" + ], + [ + 0.2, + "rgb(214,96,77)" + ], + [ + 0.3, + "rgb(244,165,130)" + ], + [ + 0.4, + "rgb(253,219,199)" + ], + [ + 0.5, + "rgb(247,247,247)" + ], + [ + 0.6, + "rgb(209,229,240)" + ], + [ + 0.7, + "rgb(146,197,222)" + ], + [ + 0.8, + "rgb(67,147,195)" + ], + [ + 0.9, + "rgb(33,102,172)" + ], + [ + 1, + "rgb(5,48,97)" + ] + ], + "geo": "geo", + "locationmode": "USA-states", + "locations": [ + "AL", + "AK", + "AZ", + "AR", + "CA", + "CO", + "CT", + "DE", + "DC", + "FL", + "GA", + "HI", + "ID", + "IL", + "IN", + "IA", + "KS", + "KY", + "LA", + "ME", + "MD", + "MA", + "MI", + "MN", + "MS", + "MO", + "MT", + "NE", + "NV", + "NH", + "NJ", + "NM", + "NY", + "NC", + "ND", + "OH", + "OK", + "OR", + "PA", + "RI", + "SC", + "SD", + "TN", + "TX", + "UT", + "VT", + "VA", + "WA", + "WV", + "WI", + "WY" + ], + "type": "choropleth", + "z": [ + 0.7593510690172818, + 0.31124808610315974, + 0.30293564229073244, + 0.6880797699955434, + 0.4076767262728649, + 0.47099722331202865, + 0.3495485539961106, + 0.6311540404546327, + 0.027585450831506653, + 0.48083360308025236, + 0.8368383466311904, + -0.06677733501972788, + 0.43909135510558733, + 0.779600877139624, + 0.7509249831955331, + 0.5724999231203786, + 0.628546384356851, + 0.8125444623615116, + 0.727275891604273, + 0.45666463509151023, + 0.6638444190101243, + 0.3137608649829189, + 0.6069199261533034, + 0.790210024997629, + 0.43188995493708615, + 0.9119906648507736, + 0.5645674083257206, + 0.7052946124028056, + 0.5077369014802505, + 0.2597717249137073, + 0.4561518780736872, + 0.2159620116218442, + 0.5240279817777199, + 0.7684137041503927, + 0.4623016512324024, + 0.763045065170108, + 0.5851282999907702, + 0.6178349533799017, + 0.7485130721362218, + 0.30082179628071476, + 0.5317292409630936, + 0.3563965756706032, + 0.8983258710857518, + 0.6689011430578589, + 0.47842328455620486, + 0.4093664774453482, + 0.7043549458150344, + 0.5274404555261272, + 0.6848858879369324, + 0.6541921545618559, + 0.42027803340897774 + ], + "zmax": 1, + "zmin": -1 + } + ], + "layout": { + "annotations": [ + { + "font": { + "size": 16 + }, + "showarrow": false, + "text": "Density polarization (most dense margin - least dense margin)", + "x": 0.5, + "xanchor": "center", + "xref": "paper", + "y": 1, + "yanchor": "bottom", + "yref": "paper" + } + ], + "autosize": false, + "geo": { + "domain": { + "x": [ + 0, + 1 + ], + "y": [ + 0, + 1 + ] + }, + "lakecolor": "rgb(255, 255, 255)", + "projection": { + "type": "albers usa" + }, + "showlakes": true + }, + "height": 560, + "template": { + "data": { + "bar": [ + { + "error_x": { + "color": "#2a3f5f" + }, + "error_y": { + "color": "#2a3f5f" + }, + "marker": { + "line": { + "color": "#E5ECF6", + "width": 0.5 + }, + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "bar" + } + ], + "barpolar": [ + { + "marker": { + "line": { + "color": "#E5ECF6", + "width": 0.5 + }, + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "barpolar" + } + ], + "carpet": [ + { + "aaxis": { + "endlinecolor": "#2a3f5f", + "gridcolor": "white", + "linecolor": "white", + "minorgridcolor": "white", + "startlinecolor": "#2a3f5f" + }, + "baxis": { + "endlinecolor": "#2a3f5f", + "gridcolor": "white", + "linecolor": "white", + "minorgridcolor": "white", + "startlinecolor": "#2a3f5f" + }, + "type": "carpet" + } + ], + "choropleth": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "choropleth" + } + ], + "contour": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "contour" + } + ], + "contourcarpet": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "contourcarpet" + } + ], + "heatmap": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "heatmap" + } + ], + "heatmapgl": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "heatmapgl" + } + ], + "histogram": [ + { + "marker": { + "pattern": { + "fillmode": "overlay", + "size": 10, + "solidity": 0.2 + } + }, + "type": "histogram" + } + ], + "histogram2d": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "histogram2d" + } + ], + "histogram2dcontour": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "histogram2dcontour" + } + ], + "mesh3d": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "type": "mesh3d" + } + ], + "parcoords": [ + { + "line": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "parcoords" + } + ], + "pie": [ + { + "automargin": true, + "type": "pie" + } + ], + "scatter": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatter" + } + ], + "scatter3d": [ + { + "line": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatter3d" + } + ], + "scattercarpet": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattercarpet" + } + ], + "scattergeo": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattergeo" + } + ], + "scattergl": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattergl" + } + ], + "scattermapbox": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scattermapbox" + } + ], + "scatterpolar": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterpolar" + } + ], + "scatterpolargl": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterpolargl" + } + ], + "scatterternary": [ + { + "marker": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "type": "scatterternary" + } + ], + "surface": [ + { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + }, + "colorscale": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "type": "surface" + } + ], + "table": [ + { + "cells": { + "fill": { + "color": "#EBF0F8" + }, + "line": { + "color": "white" + } + }, + "header": { + "fill": { + "color": "#C8D4E3" + }, + "line": { + "color": "white" + } + }, + "type": "table" + } + ] + }, + "layout": { + "annotationdefaults": { + "arrowcolor": "#2a3f5f", + "arrowhead": 0, + "arrowwidth": 1 + }, + "autotypenumbers": "strict", + "coloraxis": { + "colorbar": { + "outlinewidth": 0, + "ticks": "" + } + }, + "colorscale": { + "diverging": [ + [ + 0, + "#8e0152" + ], + [ + 0.1, + "#c51b7d" + ], + [ + 0.2, + "#de77ae" + ], + [ + 0.3, + "#f1b6da" + ], + [ + 0.4, + "#fde0ef" + ], + [ + 0.5, + "#f7f7f7" + ], + [ + 0.6, + "#e6f5d0" + ], + [ + 0.7, + "#b8e186" + ], + [ + 0.8, + "#7fbc41" + ], + [ + 0.9, + "#4d9221" + ], + [ + 1, + "#276419" + ] + ], + "sequential": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ], + "sequentialminus": [ + [ + 0, + "#0d0887" + ], + [ + 0.1111111111111111, + "#46039f" + ], + [ + 0.2222222222222222, + "#7201a8" + ], + [ + 0.3333333333333333, + "#9c179e" + ], + [ + 0.4444444444444444, + "#bd3786" + ], + [ + 0.5555555555555556, + "#d8576b" + ], + [ + 0.6666666666666666, + "#ed7953" + ], + [ + 0.7777777777777778, + "#fb9f3a" + ], + [ + 0.8888888888888888, + "#fdca26" + ], + [ + 1, + "#f0f921" + ] + ] + }, + "colorway": [ + "#636efa", + "#EF553B", + "#00cc96", + "#ab63fa", + "#FFA15A", + "#19d3f3", + "#FF6692", + "#B6E880", + "#FF97FF", + "#FECB52" + ], + "font": { + "color": "#2a3f5f" + }, + "geo": { + "bgcolor": "white", + "lakecolor": "white", + "landcolor": "#E5ECF6", + "showlakes": true, + "showland": true, + "subunitcolor": "white" + }, + "hoverlabel": { + "align": "left" + }, + "hovermode": "closest", + "mapbox": { + "style": "light" + }, + "paper_bgcolor": "white", + "plot_bgcolor": "#E5ECF6", + "polar": { + "angularaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "bgcolor": "#E5ECF6", + "radialaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + } + }, + "scene": { + "xaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + }, + "yaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + }, + "zaxis": { + "backgroundcolor": "#E5ECF6", + "gridcolor": "white", + "gridwidth": 2, + "linecolor": "white", + "showbackground": true, + "ticks": "", + "zerolinecolor": "white" + } + }, + "shapedefaults": { + "line": { + "color": "#2a3f5f" + } + }, + "ternary": { + "aaxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "baxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + }, + "bgcolor": "#E5ECF6", + "caxis": { + "gridcolor": "white", + "linecolor": "white", + "ticks": "" + } + }, + "title": { + "x": 0.05 + }, + "xaxis": { + "automargin": true, + "gridcolor": "white", + "linecolor": "white", + "ticks": "", + "title": { + "standoff": 15 + }, + "zerolinecolor": "white", + "zerolinewidth": 2 + }, + "yaxis": { + "automargin": true, + "gridcolor": "white", + "linecolor": "white", + "ticks": "", + "title": { + "standoff": 15 + }, + "zerolinecolor": "white", + "zerolinewidth": 2 + } + } + }, + "width": 800 + } + }, + "text/html": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "plot_several_by_states(\n", + " states,\n", + " np.array([by_state[:, -1] - by_state[:, 0]]),\n", + " [\"Density polarization (most dense margin - least dense margin)\"],\n", + " aspect=0.7,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7d0beec0", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "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.7.10" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/urbanstats/unrelated_projects/__init__.py b/urbanstats/unrelated_projects/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/urbanstats/unrelated_projects/density_and_partisanship.py b/urbanstats/unrelated_projects/density_and_partisanship.py new file mode 100644 index 00000000..f4aa7a0f --- /dev/null +++ b/urbanstats/unrelated_projects/density_and_partisanship.py @@ -0,0 +1,120 @@ +from typing import List +import us +import numpy as np + +from election_data import with_election_results +from wquantiles import quantile + +partisanship_columns = [ + ("2020 Presidential Election", "dem"), + ("2020 Presidential Election", "gop"), + ("2020 Presidential Election", "total"), +] + + +def geoid_to_state(x): + prefix = "7500000US" + assert x.startswith(prefix) + return x[len(prefix) : len(prefix) + 2] + + +def margin(d, g, t): + return (d - g) / t + + +def load_data(n): + data = with_election_results() + data = data[data[partisanship_columns[-1]] > 0].copy() + data["state"] = data.geoid.apply(geoid_to_state) + data = data.rename(columns={"ad_1": "density"}) + data.density /= data.population + bins = compute_bins(data.total, data.density, n=n) + data["bin_idxs"] = bin_idx(bins, data.density) + return bins, data + + +def compute_bins(pop, dens, n): + return [-np.inf] + [quantile(dens, pop, i / n) for i in range(1, n)] + [np.inf] + + +def compute_by_bin(bins, dgt, dens): + return [ + margin(*dgt[(low < dens) & (dens <= high)].sum(0)) + for low, high in zip(bins, bins[1:]) + ] + + +def bin_idx(bins, dens): + out = np.zeros_like(dens, dtype=np.int16) + for i, (low, high) in enumerate(zip(bins, bins[1:])): + out[(low < dens) & (dens <= high)] = i + 1 + return out + + +size = 800 + + +def plot_by_state(states: List[str], values: np.ndarray, scale): + # choropleth + import plotly.graph_objects as go + + # fig = px.choropleth( + # locations=[us.states.lookup(s).abbr for s in states], + # locationmode="USA-states", + # scope="usa", + # color=values, + # color_continuous_scale="RdBu", + # range_color=(-scale, scale), + # labels={"color": "Margin"}, + # title=title, + # ) + + # return fig + chor = go.Choropleth( + z=values, + locationmode="USA-states", + locations=[us.states.lookup(s).abbr for s in states], + colorscale="RdBu", + zmin=-scale, + zmax=scale, + colorbar_title="Margin", + ) + return chor + + +def plot_several_by_states(states, all_values: List[np.ndarray], titles: List[str], aspect=0.4): + from plotly.subplots import make_subplots + import plotly.graph_objects as go + + fig = make_subplots( + rows=len(all_values), + cols=1, + subplot_titles=titles, + shared_yaxes=True, + specs=[[{"type": "choropleth"}]] * len(all_values), + horizontal_spacing = 0.05, + vertical_spacing = 0.05, + ) + for i, values in enumerate(all_values): + fig.add_trace(plot_by_state(states, values, 1), row=i + 1, col=1) + fig.update_geos( + projection_type="albers usa", + showlakes=True, + lakecolor="rgb(255, 255, 255)", + ) + fig.update_layout( + autosize=False, + width=size, + height=size * len(all_values) * aspect, + ) + + return fig + +def plot_density_categories(states, by_state, suffix=""): + assert by_state.shape[1] == 3 + fig = plot_several_by_states( + states, + list(by_state.T), + ["Among " + x + ": Margin" + suffix for x in ["Least Dense", "Middle Dense", "Most Dense"]] + ) + return fig \ No newline at end of file