{"spec_id":"map-tilegrid","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nmap-tilegrid: Tile Grid Map for Equal-Area Geographic Comparison\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 86/100 | Created: 2026-05-14\n\"\"\"\n\nimport os\n\nimport matplotlib\nimport matplotlib.patches as mpatches\nimport matplotlib.pyplot as plt\nfrom matplotlib.cm import ScalarMappable\nfrom matplotlib.colors import Normalize\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# US states tile grid layout: (abbreviation, row, col, renewable_energy_pct)\nstates = [\n    (\"ME\", 0, 10, 72),\n    (\"MT\", 1, 3, 65),\n    (\"ND\", 1, 4, 54),\n    (\"MN\", 1, 5, 48),\n    (\"WI\", 1, 6, 22),\n    (\"MI\", 1, 7, 18),\n    (\"VT\", 1, 9, 88),\n    (\"NH\", 1, 10, 32),\n    (\"WA\", 2, 0, 84),\n    (\"ID\", 2, 1, 78),\n    (\"WY\", 2, 2, 15),\n    (\"SD\", 2, 3, 81),\n    (\"IA\", 2, 4, 62),\n    (\"IL\", 2, 5, 24),\n    (\"IN\", 2, 6, 11),\n    (\"OH\", 2, 7, 10),\n    (\"PA\", 2, 8, 16),\n    (\"NY\", 2, 9, 38),\n    (\"MA\", 2, 10, 19),\n    (\"OR\", 3, 0, 72),\n    (\"NV\", 3, 1, 42),\n    (\"CO\", 3, 2, 35),\n    (\"NE\", 3, 3, 43),\n    (\"MO\", 3, 4, 17),\n    (\"KY\", 3, 5, 9),\n    (\"WV\", 3, 6, 6),\n    (\"VA\", 3, 7, 21),\n    (\"NJ\", 3, 8, 11),\n    (\"CT\", 3, 9, 14),\n    (\"RI\", 3, 10, 22),\n    (\"CA\", 4, 0, 58),\n    (\"UT\", 4, 1, 24),\n    (\"AZ\", 4, 2, 38),\n    (\"KS\", 4, 3, 55),\n    (\"AR\", 4, 4, 18),\n    (\"TN\", 4, 5, 16),\n    (\"NC\", 4, 6, 24),\n    (\"SC\", 4, 7, 18),\n    (\"MD\", 4, 8, 12),\n    (\"DE\", 4, 9, 10),\n    (\"NM\", 5, 2, 45),\n    (\"OK\", 5, 3, 52),\n    (\"LA\", 5, 4, 11),\n    (\"MS\", 5, 5, 8),\n    (\"AL\", 5, 6, 13),\n    (\"GA\", 5, 7, 17),\n    (\"TX\", 6, 3, 28),\n    (\"FL\", 6, 7, 20),\n    (\"DC\", 6, 8, 5),\n    (\"AK\", 7, 0, 30),\n    (\"HI\", 7, 1, 36),\n]\n\n# Color mapping — sequential viridis for unipolar renewable energy data\nvalues = [v for _, _, _, v in states]\nvmin, vmax = min(values), max(values)\nnorm = Normalize(vmin=vmin, vmax=vmax)\ncmap = matplotlib.colormaps[\"viridis\"]\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nTILE = 0.82\nPAD = (1.0 - TILE) / 2.0\n\nfor abbr, row, col, value in states:\n    tile_color = cmap(norm(value))\n    rect = mpatches.Rectangle(\n        (col + PAD, -(row + 1) + PAD), TILE, TILE, facecolor=tile_color, edgecolor=PAGE_BG, linewidth=2.0, zorder=2\n    )\n    ax.add_patch(rect)\n\n    r_c, g_c, b_c = tile_color[:3]\n    lum = 0.2126 * r_c + 0.7152 * g_c + 0.0722 * b_c\n    label_color = \"#1A1A17\" if lum > 0.45 else \"#F0EFE8\"\n\n    ax.text(\n        col + 0.5,\n        -(row + 0.5),\n        abbr,\n        ha=\"center\",\n        va=\"center\",\n        fontsize=13,\n        fontweight=\"bold\",\n        color=label_color,\n        zorder=3,\n    )\n\n# Axes bounds — grid spans cols 0–10 (12 units) × rows 0–7 (8.5 units)\nax.set_xlim(-0.5, 11.5)\nax.set_ylim(-8.5, 0.5)\nax.set_aspect(\"equal\", adjustable=\"box\")\nax.axis(\"off\")\n\n# Colorbar\nsm = ScalarMappable(cmap=cmap, norm=norm)\nsm.set_array([])\ncbar = plt.colorbar(sm, ax=ax, fraction=0.025, pad=0.04, shrink=0.75, aspect=25)\ncbar.set_label(\"Renewable Energy Share (%)\", fontsize=16, color=INK_SOFT, labelpad=10)\ncbar.ax.tick_params(labelsize=13, labelcolor=INK_SOFT, colors=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\n\n# Title\nax.set_title(\n    \"US Renewable Energy Share · map-tilegrid · matplotlib · anyplot.ai\",\n    fontsize=22,\n    fontweight=\"medium\",\n    color=INK,\n    pad=15,\n)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}