{"spec_id":"map-tilegrid","library":"makie","language":"julia","code":"# anyplot.ai\n# map-tilegrid: Tile Grid Map for Equal-Area Geographic Comparison\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 91/100 | Created: 2026-09-05\n\nusing CairoMakie\nusing ColorSchemes\nusing Colors\nusing Random\n\nRandom.seed!(42)\n\n# --- Theme tokens ------------------------------------------------------------\nconst THEME    = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG  = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst INK      = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\n\n# Continuous, single-polarity metric (share of electricity, %) -> imprint_seq\nconst ANYPLOT_SEQ = cgrad([colorant\"#009E73\", colorant\"#4467A3\"])\nconst TILE_LABEL  = colorant\"#FAF8F1\"  # constant across themes: sits on data color, not page bg\nconst TILE_OUTLINE = colorant\"#1A1A17\"  # constant across themes: text outline for legibility\n\n# --- Data ----------------------------------------------------------------\n# European countries laid out on an equal-area tile grid, approximating true\n# geographic position (row 0 = north, col 0 = west). Renewable electricity\n# share is a unipolar metric, so it uses the sequential Imprint colormap.\ncodes = [\n    \"IS\", \"NO\", \"SE\", \"FI\",\n    \"IE\", \"UK\", \"DK\", \"EE\",\n    \"NL\", \"DE\", \"CZ\", \"PL\", \"LV\",\n    \"BE\", \"FR\", \"CH\", \"AT\", \"SK\", \"LT\",\n    \"PT\", \"ES\", \"IT\", \"HU\", \"RO\",\n    \"HR\", \"GR\", \"BG\",\n]\nrows = [\n    0, 0, 0, 0,\n    1, 1, 1, 1,\n    2, 2, 2, 2, 2,\n    3, 3, 3, 3, 3, 3,\n    4, 4, 4, 4, 4,\n    5, 5, 5,\n]\ncols = [\n    0, 3, 4, 5,\n    0, 1, 4, 6,\n    1, 3, 4, 5, 6,\n    1, 2, 3, 4, 5, 6,\n    0, 1, 3, 4, 5,\n    3, 5, 6,\n]\nbase_renewable_pct = [\n    100.0, 98.0, 75.0, 55.0,\n    40.0, 43.0, 65.0, 25.0,\n    33.0, 46.0, 18.0, 17.0, 45.0,\n    25.0, 27.0, 75.0, 78.0, 20.0, 30.0,\n    61.0, 42.0, 35.0, 15.0, 42.0,\n    40.0, 30.0, 20.0,\n]\nrenewable_pct = clamp.(\n    round.(base_renewable_pct .+ (rand(length(base_renewable_pct)) .- 0.5) .* 6, digits=1),\n    0.0, 100.0,\n)\n\nn_cols = maximum(cols) + 1\nn_rows = maximum(rows) + 1\nvalue_min, value_max = extrema(renewable_pct)\nnorm_values = (renewable_pct .- value_min) ./ (value_max - value_min)\ntile_colors = [get(ANYPLOT_SEQ, t) for t in norm_values]\n\ntile_gap = 0.05\ntile_rects = [\n    Rect2f(c + tile_gap, -(r + 1 - tile_gap), 1 - 2tile_gap, 1 - 2tile_gap)\n    for (r, c) in zip(rows, cols)\n]\nlabel_positions = [Point2f(c + 0.5, -(r + 0.5)) for (r, c) in zip(rows, cols)]\n\n# Highlight the highest/lowest performers to give the color gradient an\n# explicit focal point, rather than relying on the gradient alone.\nmax_idx = argmax(renewable_pct)\nmin_idx = argmin(renewable_pct)\nstroke_colors = fill(PAGE_BG, length(tile_rects))\nstroke_widths = fill(5.0, length(tile_rects))\nstroke_colors[max_idx] = INK\nstroke_colors[min_idx] = INK\nstroke_widths[max_idx] = 6.0\nstroke_widths[min_idx] = 6.0\n\n# --- Plot ----------------------------------------------------------------\nfig = Figure(\n    size            = (1200, 1200),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title           = \"map-tilegrid · julia · makie · anyplot.ai\",\n    titlesize       = 20,\n    titlecolor      = INK,\n    backgroundcolor = PAGE_BG,\n    aspect          = DataAspect(),\n)\nhidedecorations!(ax)\nhidespines!(ax)\nxlims!(ax, -0.3, n_cols + 0.3)\nylims!(ax, -(n_rows + 0.3), 0.3)\n\npoly!(ax, tile_rects; color = tile_colors, strokecolor = stroke_colors, strokewidth = stroke_widths)\ntext!(\n    ax, label_positions;\n    text        = codes,\n    align       = (:center, :center),\n    fontsize    = 17,\n    color       = TILE_LABEL,\n    strokecolor = TILE_OUTLINE,\n    strokewidth = 1,\n)\ntext!(\n    ax, Point2f(n_cols / 2, 0.15);\n    text     = \"Highest: $(codes[max_idx]) ($(renewable_pct[max_idx])%)   ·   Lowest: $(codes[min_idx]) ($(renewable_pct[min_idx])%)\",\n    align    = (:center, :center),\n    fontsize = 13,\n    color    = INK_SOFT,\n)\n\nColorbar(\n    fig[2, 1];\n    colormap      = ANYPLOT_SEQ,\n    limits        = (value_min, value_max),\n    vertical      = false,\n    flipaxis      = false,\n    label         = \"Renewable Energy Share (%)\",\n    labelcolor    = INK,\n    labelsize     = 14,\n    ticklabelsize = 12,\n    ticklabelcolor = INK_SOFT,\n    tickcolor     = INK_SOFT,\n    height        = 40,\n)\nrowsize!(fig.layout, 1, Relative(0.88))\nrowgap!(fig.layout, 18)\n\n# --- Save ------------------------------------------------------------------\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}