{"spec_id":"scatter-lag","library":"makie","language":"julia","code":"# anyplot.ai\n# scatter-lag: Lag Plot for Time Series Autocorrelation Diagnosis\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 88/100 | Created: 2026-06-24\n\nusing CairoMakie\nusing Colors\nusing ColorSchemes\nusing Random\nusing Statistics\n\nRandom.seed!(42)\n\n# Theme tokens (Imprint palette — see prompts/default-style-guide.md)\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# Sequential Imprint colormap for time-index coloring (green → blue)\nconst ANYPLOT_SEQ = cgrad([colorant\"#009E73\", colorant\"#4467A3\"])\n\n# Makie-distinctive: base theme for tick alignment and spine weight\nconst ANYPLOT_MAKIE_THEME = Theme(\n    Axis = (\n        xtickalign = 1,\n        ytickalign = 1,\n        spinewidth  = 0.7,\n    ),\n)\n\n# Data: AR(1) process with positive autocorrelation (phi = 0.85)\nn = 500\nphi = 0.85\nts = zeros(n)\nts[1] = randn()\nfor i in 2:n\n    ts[i] = phi * ts[i-1] + 0.5 * randn()\nend\n\n# Lag-1 scatter preparation\nlag = 1\ny_t  = ts[1:end-lag]\ny_t1 = ts[1+lag:end]\nn_pts = length(y_t)\n\n# Normalize time index to [0, 1] for sequential color mapping\ntime_color = collect(1:n_pts) ./ n_pts\n\n# Pearson correlation coefficient at lag 1\nr_val = cor(y_t, y_t1)\n\nwith_theme(ANYPLOT_MAKIE_THEME) do\n    fig = Figure(\n        size            = (1600, 900),\n        fontsize        = 14,\n        backgroundcolor = PAGE_BG,\n    )\n\n    ax = Axis(\n        fig[1, 1];\n        title              = \"scatter-lag · julia · makie · anyplot.ai\",\n        titlesize          = 22,\n        titlecolor         = INK,\n        xlabel             = \"y(t)\",\n        ylabel             = \"y(t + 1)\",\n        xlabelsize         = 14,\n        ylabelsize         = 14,\n        xlabelcolor        = INK,\n        ylabelcolor        = INK,\n        xticklabelsize     = 12,\n        yticklabelsize     = 12,\n        xticklabelcolor    = INK_SOFT,\n        yticklabelcolor    = INK_SOFT,\n        xtickcolor         = INK_SOFT,\n        ytickcolor         = INK_SOFT,\n        backgroundcolor    = PAGE_BG,\n        topspinevisible    = false,\n        rightspinevisible  = false,\n        leftspinecolor     = INK_SOFT,\n        bottomspinecolor   = INK_SOFT,\n        xgridcolor         = RGBAf(INK.r, INK.g, INK.b, 0.15),\n        ygridcolor         = RGBAf(INK.r, INK.g, INK.b, 0.15),\n        xminorgridvisible  = false,\n        yminorgridvisible  = false,\n        xtickformat        = values -> [\"$(round(v; digits=1))\" for v in values],\n        ytickformat        = values -> [\"$(round(v; digits=1))\" for v in values],\n    )\n\n    # Diagonal reference line y = x\n    all_vals = vcat(y_t, y_t1)\n    lo = minimum(all_vals) - 0.3\n    hi = maximum(all_vals) + 0.3\n    ref_line = range(lo, hi; length = 100)\n    lines!(ax, collect(ref_line), collect(ref_line);\n        color     = INK_SOFT,\n        linewidth = 1.5,\n        linestyle = :dash,\n    )\n\n    # Scatter colored by time index using the Imprint sequential colormap\n    sc = scatter!(ax, y_t, y_t1;\n        color       = time_color,\n        colormap    = ANYPLOT_SEQ,\n        colorrange  = (0.0, 1.0),\n        markersize  = 7,\n        alpha       = 0.72,\n        strokewidth = 0,\n    )\n\n    # Colorbar labeling the temporal axis\n    Colorbar(fig[1, 2], sc;\n        label          = \"Time\",\n        labelcolor     = INK,\n        tickcolor      = INK_SOFT,\n        ticklabelcolor = INK_SOFT,\n        width          = 14,\n        tellheight     = false,\n        ticks          = ([0.0, 1.0], [\"Early\", \"Late\"]),\n    )\n\n    # Correlation coefficient annotation in data coordinates\n    ann_x = minimum(y_t)  + 0.04 * (maximum(y_t)  - minimum(y_t))\n    ann_y = minimum(y_t1) + 0.93 * (maximum(y_t1) - minimum(y_t1))\n    text!(ax, \"r = $(round(r_val; digits = 2))\";\n        position = (ann_x, ann_y),\n        fontsize = 13,\n        color    = INK,\n        font     = :bold,\n    )\n\n    # Save\n    save(\"plot-$(THEME).png\", fig; px_per_unit = 2)\nend\n"}