{"spec_id":"line-arrhenius","library":"makie","language":"julia","code":"# anyplot.ai\n# line-arrhenius: Arrhenius Plot for Reaction Kinetics\n# Library: makie 0.22.10 | Julia 1.11.9\n# Quality: 84/100 | Created: 2026-06-24\n\nusing CairoMakie\nusing Colors\nusing Random\nusing Statistics\nusing Printf\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 ELEVATED_BG = THEME == \"light\" ? colorant\"#FFFDF6\" : colorant\"#242420\"\nconst INK         = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT    = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\n\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\", colorant\"#C475FD\", colorant\"#4467A3\", colorant\"#BD8233\",\n    colorant\"#AE3030\", colorant\"#2ABCCD\", colorant\"#954477\", colorant\"#99B314\",\n]\n\n# Arrhenius data: first-order thermal decomposition\n# k = A * exp(-Ea / R / T),  Ea = 80 kJ/mol,  A = 1e10 s⁻¹\nconst R_GAS = 8.314     # J mol⁻¹ K⁻¹\nconst EA    = 80_000.0  # J mol⁻¹\nconst A_PRE = 1.0e10    # s⁻¹\n\ntemperatures_K = [300.0, 330.0, 360.0, 400.0, 440.0, 480.0, 520.0, 560.0, 600.0]\nk_ideal        = A_PRE .* exp.(-EA ./ (R_GAS .* temperatures_K))\nrate_constants = k_ideal .* (1.0 .+ 0.04 .* randn(length(temperatures_K)))\n\n# Arrhenius linearisation using 1000/T on x-axis (conventional; avoids tiny tick numbers)\ninv_T_scaled = 1000.0 ./ temperatures_K  # values ≈ 1.67 – 3.33\nln_k         = log.(rate_constants)\n\n# Linear regression: ln(k) = slope * (1000/T) + intercept\nx_bar = mean(inv_T_scaled)\ny_bar = mean(ln_k)\nslope     = sum((inv_T_scaled .- x_bar) .* (ln_k .- y_bar)) /\n            sum((inv_T_scaled .- x_bar).^2)\nintercept = y_bar - slope * x_bar\n\nln_k_fit = slope .* inv_T_scaled .+ intercept\nr_sq     = 1.0 - sum((ln_k .- ln_k_fit).^2) / sum((ln_k .- y_bar).^2)\n\n# Ea from slope: since x = 1000/T, slope × 1000 = -Ea/R\nea_kJmol = -slope * R_GAS  # ≈ 79.9 kJ/mol\n\n# Extended regression line\nmargin = 0.08 * (maximum(inv_T_scaled) - minimum(inv_T_scaled))\nx_fit  = collect(range(minimum(inv_T_scaled) - margin,\n                       maximum(inv_T_scaled) + margin; length = 200))\ny_fit  = slope .* x_fit .+ intercept\n\n# 95% confidence band (t_{0.025, df=7} = 2.365)\nn_pts   = length(inv_T_scaled)\nsse     = sum((ln_k .- ln_k_fit).^2)\ns2      = sse / (n_pts - 2)\nSxx     = sum((inv_T_scaled .- x_bar).^2)\nt_crit  = 2.365\nse_band = sqrt.(s2 .* (1.0 / n_pts .+ (x_fit .- x_bar).^2 ./ Sxx))\ny_upper = y_fit .+ t_crit .* se_band\ny_lower = y_fit .- t_crit .* se_band\n\ntitle_str  = \"line-arrhenius · julia · makie · anyplot.ai\"\ntitle_size = length(title_str) > 67 ? max(14, round(Int, 20 * 67 / length(title_str))) : 20\n\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title              = title_str,\n    titlesize          = title_size,\n    titlecolor         = INK,\n    xlabel             = \"10³/T  (K⁻¹)\",\n    ylabel             = \"ln(k)\",\n    xlabelsize         = 16,\n    ylabelsize         = 16,\n    xticklabelsize     = 13,\n    yticklabelsize     = 13,\n    xlabelcolor        = INK,\n    ylabelcolor        = INK,\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    xgridvisible       = false,\n    ygridvisible       = true,\n    xminorgridvisible  = false,\n    yminorgridvisible  = false,\n    ygridcolor         = RGBAf(INK.r, INK.g, INK.b, 0.12),\n    xticks             = LinearTicks(4),\n)\n\n# True secondary x-axis: temperature reference in K (overlapping Axis at top)\nax_top = Axis(\n    fig[1, 1];\n    xaxisposition      = :top,\n    yaxisposition      = :right,\n    backgroundcolor    = :transparent,\n    topspinevisible    = true,\n    topspinecolor      = INK_SOFT,\n    bottomspinevisible = false,\n    leftspinevisible   = false,\n    rightspinevisible  = false,\n    xgridvisible       = false,\n    ygridvisible       = false,\n    yticksvisible      = false,\n    yticklabelsvisible = false,\n    xlabel             = \"Temperature (K)\",\n    xlabelsize         = 13,\n    xlabelcolor        = INK_SOFT,\n    xticklabelsize     = 11,\n    xticklabelcolor    = INK_SOFT,\n    xtickcolor         = INK_SOFT,\n    xticks             = (\n        1000.0 ./ [300.0, 350.0, 400.0, 500.0, 600.0],\n        [\"300\", \"350\", \"400\", \"500\", \"600\"],\n    ),\n)\nlinkxaxes!(ax, ax_top)\nlinkyaxes!(ax, ax_top)\n\n# Confidence band (Makie band! — highlights fit quality)\nband!(ax, x_fit, y_lower, y_upper;\n    color = (IMPRINT_PALETTE[3], 0.15),\n)\n\n# Regression line (drawn before markers so they render on top)\nlines!(ax, x_fit, y_fit;\n    color     = IMPRINT_PALETTE[3],\n    linewidth = 2.5,\n    linestyle = :dash,\n    label     = \"Linear fit (Arrhenius)\",\n)\n\n# Experimental data points\nscatter!(ax, inv_T_scaled, ln_k;\n    color       = IMPRINT_PALETTE[1],\n    markersize  = 14,\n    strokewidth = 1.5,\n    strokecolor = PAGE_BG,\n    label       = \"Measured k(T)\",\n)\n\n# Annotation: kinetic parameters — lower-left corner\nx_range = maximum(inv_T_scaled) - minimum(inv_T_scaled)\ny_range = maximum(ln_k) - minimum(ln_k)\nx_ann   = minimum(inv_T_scaled) + 0.03 * x_range\ny_ann   = minimum(ln_k) + 0.08 * y_range\n\ntext!(ax, x_ann, y_ann;\n    text     = @sprintf(\"-Ea/R = %d K\\nEa = %.1f kJ mol⁻¹\\nR² = %.4f\",\n                   round(Int, -slope * 1000.0), ea_kJmol, r_sq),\n    color    = INK,\n    fontsize = 12,\n    align    = (:left, :bottom),\n)\n\naxislegend(ax;\n    position        = :rt,\n    labelsize       = 12,\n    framevisible    = true,\n    framecolor      = INK_SOFT,\n    backgroundcolor = ELEVATED_BG,\n    labelcolor      = INK,\n)\n\nresize_to_layout!(fig)\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}