{"spec_id":"map-tilegrid","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nmap-tilegrid: Tile Grid Map for Equal-Area Geographic Comparison\nLibrary: letsplot 4.11.0 | Python 3.14.10\nQuality: 81/100 | Updated: 2026-11-27\n\"\"\"\n\nimport math\nimport os\nfrom itertools import pairwise\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\n\n\nLetsPlot.setup_html()\n\n# Theme tokens (see prompts/default-style-guide.md \"Background\" + \"Theme-adaptive Chrome\")\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# Imprint palette: all eight positions in canonical order\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Imprint sequential ramp for fields (colored cells that fill the plot), as (position, hex) stops\nSEQ_FIELDS = (\n    [(0.0, \"#66E2B2\"), (0.369, \"#009E73\"), (0.803, \"#024582\"), (1.0, \"#351E45\")]\n    if THEME == \"light\"\n    else [(0.0, \"#404C02\"), (0.487, \"#009E73\"), (1.0, \"#CDD9FF\")]\n)\n\n\ndef ramp_color(stops, t):\n    \"\"\"The hex at t (0 to 1) along (position, hex) stops, blended linearly in sRGB.\"\"\"\n    if not math.isfinite(t):\n        return \"none\"\n    t = min(max(t, 0.0), 1.0)\n    (p0, c0), (p1, c1) = next((a, b) for a, b in pairwise(stops) if t <= b[0])\n    share = (t - p0) / (p1 - p0)\n    return \"#\" + \"\".join(\n        f\"{round(int(c0[i : i + 2], 16) * (1 - share) + int(c1[i : i + 2], 16) * share):02X}\" for i in (1, 3, 5)\n    )\n\n\n# Columns: set these to your table's column names\nREGION = \"Region\"  # role: region\nSHARE = \"Renewable share (%)\"  # role: value\nROW = \"Grid row\"  # role: row\nCOL = \"Grid column\"  # role: col\n\n# Data: replace this block with your own table, for example df = pd.read_csv(\"tiles.csv\")\n# Renewable energy share (%) of gross final energy consumption, European countries\n# region: (grid_row, grid_col, approximate_share)\ncountries = {\n    \"IS\": (0, 2, 78),\n    \"NO\": (1, 4, 71),\n    \"SE\": (1, 5, 66),\n    \"FI\": (1, 6, 52),\n    \"IE\": (2, 0, 14),\n    \"UK\": (2, 1, 21),\n    \"DK\": (2, 4, 43),\n    \"EE\": (2, 7, 32),\n    \"NL\": (3, 1, 15),\n    \"DE\": (3, 3, 21),\n    \"PL\": (3, 4, 17),\n    \"LV\": (3, 7, 44),\n    \"BE\": (4, 1, 14),\n    \"LU\": (4, 2, 12),\n    \"CZ\": (4, 3, 18),\n    \"SK\": (4, 4, 19),\n    \"LT\": (4, 7, 29),\n    \"FR\": (5, 1, 20),\n    \"CH\": (5, 3, 30),\n    \"AT\": (5, 4, 40),\n    \"HU\": (5, 5, 16),\n    \"RO\": (5, 6, 27),\n    \"PT\": (6, 0, 39),\n    \"ES\": (6, 1, 22),\n    \"IT\": (6, 3, 21),\n    \"SI\": (6, 4, 26),\n    \"HR\": (6, 5, 32),\n    \"BG\": (6, 6, 22),\n    \"MT\": (7, 3, 13),\n    \"GR\": (7, 5, 24),\n}\nnp.random.seed(42)\nbase_share = np.array([v[2] for v in countries.values()], dtype=float)\ndf = pd.DataFrame(\n    {\n        REGION: list(countries.keys()),\n        ROW: [v[0] for v in countries.values()],\n        COL: [v[1] for v in countries.values()],\n        SHARE: np.clip(base_share + np.random.normal(0, 2.5, len(countries)), 5, 90),\n    }\n)\n\n# Plot\n# Per-tile label color: ink or off-white by the luminance of the tile's fill on the ramp\nLABEL_DARK = \"#1A1A17\"\nLABEL_LIGHT = \"#F0EFE8\"\nshare_span = df[SHARE].max() - df[SHARE].min()\nnorm_share = (df[SHARE] - df[SHARE].min()) / (share_span if share_span > 0 else 1.0)\nfill_hex = [ramp_color(SEQ_FIELDS, t) for t in norm_share]\nfill_rgb = np.array([[int(h[i : i + 2], 16) for i in (1, 3, 5)] for h in fill_hex])\nfill_luminance = fill_rgb @ np.array([0.299, 0.587, 0.114])\ndf[\"label_color\"] = np.where(fill_luminance > 120, LABEL_DARK, LABEL_LIGHT)\n\ntitle = \"Renewable Energy Share · map-tilegrid · python · letsplot · anyplot.ai\"\ntitle_fontsize = round(16 * min(1.0, 67 / len(title)))\n\nplot = (\n    ggplot(df, aes(x=COL, y=ROW))\n    + geom_tile(aes(fill=SHARE), width=0.94, height=0.94)\n    + geom_text(aes(label=REGION, color=\"label_color\"), size=6, fontface=\"bold\")\n    + scale_color_identity()\n    + scale_fill_gradientn(colors=[ramp_color(SEQ_FIELDS, k / 255) for k in range(256)], name=SHARE)\n    + scale_x_continuous(expand=[0.01, 0])\n    + scale_y_reverse(expand=[0.01, 0])\n    + coord_fixed()\n    + labs(x=\"\", y=\"\", title=title)\n)\n\nanyplot_theme = theme(\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG),\n    panel_border=element_rect(color=INK_SOFT, size=0.5),\n    panel_grid=element_blank(),\n    axis_title=element_blank(),\n    axis_text=element_blank(),\n    axis_ticks=element_blank(),\n    axis_line=element_blank(),\n    plot_title=element_text(color=INK, size=title_fontsize),\n    legend_position=\"bottom\",\n    legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n    legend_text=element_text(color=INK_SOFT, size=12),\n    legend_title=element_text(color=INK, size=13),\n)\n\n# The one-panel grid paints the whole canvas with the page color; the plot keeps its fixed aspect\nplot = (\n    gggrid([plot + anyplot_theme]) + ggsize(600, 600) + theme(plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG))\n)\n\n# Save\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}