{"spec_id":"heatmap-basic","library":"makie","language":"julia","code":"# anyplot.ai\n# heatmap-basic: Basic Heatmap\n# Library: makie 0.24.16 | Julia 1.13.4\n# Quality: 86/100 | Updated: 2026-11-27\n\nusing CairoMakie\nusing Colors\nusing DataFrames\nusing Random\nusing Printf\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# Imprint sequential ramp for filled cells (single-signed data)\nconst ANYPLOT_SEQ_FIELDS = THEME == \"light\" ?\n    cgrad([colorant\"#66E2B2\", colorant\"#009E73\", colorant\"#024582\", colorant\"#351E45\"], [0.0, 0.369, 0.803, 1.0]) :\n    cgrad([colorant\"#404C02\", colorant\"#009E73\", colorant\"#CDD9FF\"], [0.0, 0.487, 1.0])\n\n# Columns: set these to your table's column names\nconst PLAN     = \"Subscription plan\"         # role: row\nconst FEATURE  = \"Product feature\"           # role: column\nconst ADOPTION = \"Adoption (% of accounts)\"  # role: value\n\n# Data: replace this block with your own table, for example df = CSV.read(\"adoption.csv\", DataFrame)\n# Product-usage metrics: share of accounts using each feature, by plan tier\nRandom.seed!(42)\nplans    = [\"Trial\", \"Free\", \"Starter\", \"Team\", \"Business\", \"Enterprise\"]\nfeatures = [\"Dashboards\", \"Reports\", \"Alerts\", \"Export\", \"API\", \"Integrations\", \"SSO\", \"Audit log\"]\ndf = DataFrame(\n    PLAN     => repeat(plans; inner = length(features)),\n    FEATURE  => repeat(features; outer = length(plans)),\n    ADOPTION => [\n        clamp(100 / (1 + exp(0.9 * (f - 1.5 - 1.3 * p))) + 4 * randn(), 2, 98)\n        for p in eachindex(plans) for f in eachindex(features)\n    ],\n)\n\n# Plot: reads only df and the constants above\nrow_names = unique(df[!, PLAN])\ncol_names = unique(df[!, FEATURE])\nnr, nc    = length(row_names), length(col_names)\ngrid      = permutedims(reshape(df[!, ADOPTION], nc, nr))   # rows = plans, columns = features\nvmin, vmax = extrema(df[!, ADOPTION])\n\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title              = \"heatmap-basic · julia · makie · anyplot.ai\",\n    titlesize          = 22,\n    titlecolor         = INK,\n    xlabel             = FEATURE,\n    ylabel             = PLAN,\n    xlabelsize         = 18,\n    ylabelsize         = 18,\n    xlabelcolor        = INK,\n    ylabelcolor        = INK,\n    xticklabelcolor    = INK_SOFT,\n    yticklabelcolor    = INK_SOFT,\n    xticklabelsize     = 16,\n    yticklabelsize     = 16,\n    xticksize          = 0,\n    yticksize          = 0,\n    backgroundcolor    = PAGE_BG,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinevisible   = false,\n    bottomspinevisible = false,\n    xgridvisible       = false,\n    ygridvisible       = false,\n    xticks             = (1:nc, col_names),\n    yticks             = (1:nr, row_names),\n    yreversed          = true,\n)\n\nhm = heatmap!(ax, 1:nc, 1:nr, permutedims(grid);\n    colormap   = ANYPLOT_SEQ_FIELDS,\n    colorrange = (vmin, vmax),\n)\n\n# Thin page-colored separators between cells\nvlines!(ax, [i + 0.5 for i in 0:nc]; color = PAGE_BG, linewidth = 3)\nhlines!(ax, [i + 0.5 for i in 0:nr]; color = PAGE_BG, linewidth = 3)\n\n# In-cell values: ink flips with the darkness of the cell underneath\npositions   = Point2f[]\ncell_labels = String[]\nlabel_color = RGBAf[]\nfor r in 1:nr, c in 1:nc\n    v   = grid[r, c]\n    bg  = get(ANYPLOT_SEQ_FIELDS, (v - vmin) / (vmax - vmin))\n    lin(x) = x <= 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055)^2.4\n    lum = 0.2126 * lin(red(bg)) + 0.7152 * lin(green(bg)) + 0.0722 * lin(blue(bg))\n    push!(positions, Point2f(c, r))\n    push!(cell_labels, @sprintf(\"%.0f\", v))\n    # WCAG contrast of the near-black ink (L ~ 0.01) vs the near-white ink (L ~ 0.94)\n    push!(label_color, (lum + 0.05) / 0.06 > 0.99 / (lum + 0.05) ? RGBAf(0.102, 0.102, 0.090) : RGBAf(0.98, 0.973, 0.945))\nend\ntext!(ax, positions; text = cell_labels, align = (:center, :center), color = label_color, fontsize = 18)\n\nColorbar(fig[1, 2], hm;\n    label          = ADOPTION,\n    labelcolor     = INK,\n    tickcolor      = INK_SOFT,\n    ticklabelcolor = INK_SOFT,\n    ticklabelsize  = 16,\n    labelsize      = 18,\n    width          = 24,\n)\n\ncolgap!(fig.layout, 1, 16)\n\n# Save\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}