{"spec_id":"map-tilegrid","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nmap-tilegrid: Tile Grid Map for Equal-Area Geographic Comparison\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 89/100 | Created: 2026-05-14\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# US state tile grid: (row, col), row 0 = north, col 0 = west\nstate_grid = {\n    \"AK\": (0, 0),\n    \"ME\": (0, 11),\n    \"VT\": (1, 10),\n    \"NH\": (1, 11),\n    \"WA\": (2, 0),\n    \"ID\": (2, 1),\n    \"MT\": (2, 2),\n    \"ND\": (2, 3),\n    \"MN\": (2, 4),\n    \"WI\": (2, 5),\n    \"MI\": (2, 6),\n    \"NY\": (2, 9),\n    \"MA\": (2, 10),\n    \"RI\": (2, 11),\n    \"OR\": (3, 0),\n    \"NV\": (3, 1),\n    \"WY\": (3, 2),\n    \"SD\": (3, 3),\n    \"IA\": (3, 4),\n    \"IL\": (3, 5),\n    \"IN\": (3, 6),\n    \"OH\": (3, 7),\n    \"PA\": (3, 8),\n    \"NJ\": (3, 10),\n    \"CT\": (3, 11),\n    \"CA\": (4, 0),\n    \"UT\": (4, 1),\n    \"CO\": (4, 2),\n    \"NE\": (4, 3),\n    \"MO\": (4, 4),\n    \"KY\": (4, 5),\n    \"WV\": (4, 6),\n    \"VA\": (4, 7),\n    \"MD\": (4, 8),\n    \"DE\": (4, 10),\n    \"AZ\": (5, 1),\n    \"NM\": (5, 2),\n    \"KS\": (5, 3),\n    \"OK\": (5, 4),\n    \"TN\": (5, 5),\n    \"NC\": (5, 7),\n    \"SC\": (5, 8),\n    \"DC\": (5, 9),\n    \"TX\": (6, 3),\n    \"LA\": (6, 4),\n    \"AR\": (6, 5),\n    \"MS\": (6, 6),\n    \"AL\": (6, 7),\n    \"GA\": (6, 8),\n    \"FL\": (6, 9),\n    \"HI\": (7, 1),\n}\n\n# Synthetic renewable electricity share (%) with regional realism\nnp.random.seed(42)\nrenewable_base = {\n    \"WA\": 82,\n    \"OR\": 70,\n    \"ID\": 75,\n    \"MT\": 68,\n    \"VT\": 71,\n    \"ME\": 65,\n    \"SD\": 79,\n    \"ND\": 65,\n    \"IA\": 60,\n    \"CA\": 55,\n    \"HI\": 48,\n    \"KS\": 42,\n    \"OK\": 40,\n    \"NM\": 30,\n    \"NY\": 32,\n    \"NE\": 32,\n    \"CO\": 33,\n    \"MN\": 28,\n    \"AK\": 28,\n    \"TX\": 27,\n    \"MA\": 26,\n    \"NH\": 25,\n    \"TN\": 25,\n    \"UT\": 22,\n    \"FL\": 22,\n    \"SC\": 22,\n    \"AL\": 20,\n    \"VA\": 20,\n    \"WI\": 20,\n    \"RI\": 20,\n    \"MD\": 19,\n    \"CT\": 18,\n    \"AZ\": 18,\n    \"NC\": 18,\n    \"IL\": 18,\n    \"MI\": 17,\n    \"MO\": 17,\n    \"NV\": 36,\n    \"NJ\": 16,\n    \"GA\": 16,\n    \"AR\": 16,\n    \"OH\": 16,\n    \"PA\": 14,\n    \"WY\": 14,\n    \"MS\": 13,\n    \"IN\": 13,\n    \"DE\": 12,\n    \"KY\": 11,\n    \"LA\": 12,\n    \"DC\": 5,\n    \"WV\": 8,\n}\n\nn_rows, n_cols = 8, 12\ngrid = np.full((n_rows, n_cols), np.nan)\nhover_labels = np.full((n_rows, n_cols), \"\", dtype=object)\n\nfor state, (row, col) in state_grid.items():\n    base = renewable_base.get(state, 25.0)\n    grid[row, col] = float(np.clip(base + np.random.uniform(-2, 2), 5, 90))\n    hover_labels[row, col] = state\n\n# Tile label annotations — brightness-adaptive text color for viridis\nannotations = []\nfor state, (row, col) in state_grid.items():\n    val = grid[row, col]\n    # viridis: dark purple (low) → teal (mid) → bright yellow (high)\n    # switch to dark ink above ~62% where tiles become bright\n    label_color = INK_SOFT if val > 62 else \"white\"\n    annotations.append(\n        {\n            \"x\": col,\n            \"y\": row,\n            \"text\": f\"<b>{state}</b>\",\n            \"font\": {\"size\": 18, \"color\": label_color},\n            \"showarrow\": False,\n            \"xanchor\": \"center\",\n            \"yanchor\": \"middle\",\n        }\n    )\n\n# Plot\nfig = go.Figure()\n\nfig.add_trace(\n    go.Heatmap(\n        z=grid,\n        text=hover_labels,\n        hovertemplate=\"<b>%{text}</b><br>Renewable: %{z:.1f}%<extra></extra>\",\n        colorscale=\"viridis\",\n        zmin=0,\n        zmax=85,\n        xgap=3,\n        ygap=3,\n        showscale=True,\n        colorbar={\n            \"title\": {\"text\": \"Renewable<br>Energy (%)\", \"font\": {\"size\": 20, \"color\": INK}, \"side\": \"right\"},\n            \"tickfont\": {\"size\": 16, \"color\": INK_SOFT},\n            \"bgcolor\": ELEVATED_BG,\n            \"outlinecolor\": INK_SOFT,\n            \"outlinewidth\": 1,\n            \"thickness\": 30,\n            \"len\": 0.75,\n            \"x\": 1.02,\n        },\n        hoverlabel={\"bgcolor\": ELEVATED_BG, \"font\": {\"size\": 16, \"color\": INK}, \"bordercolor\": INK_SOFT},\n    )\n)\n\nfig.update_layout(\n    title={\n        \"text\": \"US Renewable Energy Share · map-tilegrid · plotly · anyplot.ai\",\n        \"subtitle\": {\n            \"text\": \"Pacific Northwest leads (70–83%) while the Southeast and DC lag far behind (5–22%)\",\n            \"font\": {\"size\": 18, \"color\": INK_SOFT},\n        },\n        \"font\": {\"size\": 28, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    xaxis={\"showgrid\": False, \"showticklabels\": False, \"showline\": False, \"zeroline\": False, \"ticks\": \"\"},\n    yaxis={\n        \"showgrid\": False,\n        \"showticklabels\": False,\n        \"showline\": False,\n        \"zeroline\": False,\n        \"ticks\": \"\",\n        \"autorange\": \"reversed\",\n    },\n    annotations=annotations,\n    margin={\"l\": 40, \"r\": 140, \"t\": 80, \"b\": 40},\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}