{"spec_id":"scatter-matrix","library":"makie","language":"julia","code":"# anyplot.ai\n# scatter-matrix: Scatter Plot Matrix\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 91/100 | Created: 2026-09-09\n\nusing CairoMakie\nusing Colors\nusing RDatasets\nusing Random\nusing Statistics\n\nRandom.seed!(42)\n\n# --- Theme tokens -------------------------------------------------------\nTHEME       = get(ENV, \"ANYPLOT_THEME\", \"light\")\nPAGE_BG     = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nELEVATED_BG = THEME == \"light\" ? colorant\"#FFFDF6\" : colorant\"#242420\"\nINK         = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nINK_SOFT    = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nGRID_COLOR  = RGBAf(INK.r, INK.g, INK.b, 0.15)\n\nIMPRINT_PALETTE = [\n    colorant\"#009E73\", colorant\"#C475FD\", colorant\"#4467A3\", colorant\"#BD8233\",\n    colorant\"#AE3030\", colorant\"#2ABCCD\", colorant\"#954477\", colorant\"#99B314\",\n]\n\n# --- Data -----------------------------------------------------------------\niris = dataset(\"datasets\", \"iris\")\n\nvariables = [:SepalLength, :SepalWidth, :PetalLength, :PetalWidth]\nvar_labels = [\"Sepal Length\", \"Sepal Width\", \"Petal Length\", \"Petal Width\"]\nn_vars = length(variables)\n\nspecies_names = unique(iris.Species)\nspecies_colors = Dict(sp => IMPRINT_PALETTE[i] for (i, sp) in enumerate(species_names))\npoint_colors = [species_colors[sp] for sp in iris.Species]\n\n# Locate the most strongly correlated pair of variables to draw the eye\n# toward a focal insight, rather than treating every panel identically.\ncor_matrix = [cor(iris[!, variables[i]], iris[!, variables[j]]) for i in 1:n_vars, j in 1:n_vars]\nbest_row, best_col, best_r = 1, 2, 0.0\nfor i in 1:n_vars, j in 1:n_vars\n    if i != j && abs(cor_matrix[i, j]) > best_r\n        global best_row, best_col, best_r = i, j, abs(cor_matrix[i, j])\n    end\nend\n\n# --- Plot -------------------------------------------------------------------\ntitle_str = \"scatter-matrix · julia · makie · anyplot.ai\"\n\nfig = Figure(\n    resolution      = (1200, 1200),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nLabel(fig[1, 1:(n_vars + 1)], title_str; fontsize = 20, color = INK, font = :bold)\n\naxes = Matrix{Axis}(undef, n_vars, n_vars)\n\nfor row in 1:n_vars, col in 1:n_vars\n    is_diag      = row == col\n    is_focal     = !is_diag && (row, col) in ((best_row, best_col), (best_col, best_row))\n    show_y       = col == 1 && !is_diag\n    show_x       = row == n_vars\n    spine_color  = is_focal ? IMPRINT_PALETTE[1] : INK_SOFT\n\n    ax = Axis(\n        fig[row + 1, col];\n        backgroundcolor  = PAGE_BG,\n        xlabel           = var_labels[col],\n        ylabel           = var_labels[row],\n        xlabelsize       = 13,\n        ylabelsize       = 13,\n        xlabelcolor      = INK,\n        ylabelcolor      = INK,\n        xlabelvisible    = show_x,\n        ylabelvisible    = show_y,\n        xticklabelsize   = 10,\n        yticklabelsize   = 10,\n        xticklabelcolor  = INK_SOFT,\n        yticklabelcolor  = INK_SOFT,\n        xticklabelsvisible = show_x,\n        yticklabelsvisible  = show_y,\n        xticksvisible    = show_x,\n        yticksvisible    = show_y,\n        xtickcolor       = INK_SOFT,\n        ytickcolor       = INK_SOFT,\n        leftspinecolor   = spine_color,\n        bottomspinecolor = spine_color,\n        topspinevisible    = is_focal,\n        rightspinevisible  = is_focal,\n        topspinecolor      = spine_color,\n        rightspinecolor    = spine_color,\n        spinewidth         = is_focal ? 2.5 : 1,\n        xgridcolor         = GRID_COLOR,\n        ygridcolor         = GRID_COLOR,\n        xminorgridvisible  = false,\n        yminorgridvisible  = false,\n    )\n    axes[row, col] = ax\n\n    if is_diag\n        ax.yticklabelsvisible = false\n        ax.ylabelvisible      = false\n        ax.yticksvisible      = false\n        for sp in species_names\n            vals = iris[iris.Species .== sp, variables[row]]\n            density!(\n                ax, vals;\n                color       = (species_colors[sp], 0.35),\n                strokecolor = species_colors[sp],\n                strokewidth = 2,\n            )\n        end\n    else\n        scatter!(\n            ax, iris[!, variables[col]], iris[!, variables[row]];\n            color       = point_colors,\n            alpha       = 0.6,\n            markersize  = 7,\n            strokewidth = 0,\n        )\n        if is_focal\n            text!(\n                ax, 0.05, 0.95;\n                text      = \"r = $(round(best_r, digits = 2))\",\n                space     = :relative,\n                align     = (:left, :top),\n                color     = IMPRINT_PALETTE[1],\n                fontsize  = 12,\n                font      = :bold,\n            )\n        end\n    end\nend\n\n# Distinctive Makie SPLOM idiom: explicitly guarantee identical per-column /\n# per-row ranges instead of relying on each Axis picking its own limits.\nfor col in 1:n_vars\n    linkxaxes!(axes[:, col]...)\nend\nfor row in 1:n_vars\n    off_diag_in_row = [axes[row, col] for col in 1:n_vars if col != row]\n    linkyaxes!(off_diag_in_row...)\nend\n\nlegend_elements = [MarkerElement(color = species_colors[sp], marker = :circle, markersize = 14) for sp in species_names]\nLegend(\n    fig[2:(n_vars + 1), n_vars + 1],\n    legend_elements, string.(species_names), \"Species\";\n    labelcolor      = INK,\n    titlecolor      = INK,\n    backgroundcolor = ELEVATED_BG,\n    framevisible    = false,\n)\n\ncolgap!(fig.layout, 8)\nrowgap!(fig.layout, 8)\n\n# --- Save -------------------------------------------------------------------\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}