{"spec_id":"scatter-regression-polynomial","library":"makie","language":"julia","code":"# anyplot.ai\n# scatter-regression-polynomial: Scatter Plot with Polynomial Regression\n# Library: makie 0.21.9 | Julia 1.11.9\n# Quality: 90/100 | Created: 2026-08-11\n\nusing CairoMakie\nusing Colors\nusing Random\nusing Statistics\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\"\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\", colorant\"#C475FD\", colorant\"#4467A3\", colorant\"#BD8233\",\n    colorant\"#AE3030\", colorant\"#2ABCCD\", colorant\"#954477\", colorant\"#99B314\",\n]\nconst BRAND = IMPRINT_PALETTE[1]\nconst FIT_COLOR = IMPRINT_PALETTE[2]\n\n# --- Data: cannon-launch trajectory measurements -------------------------------\nn_shots = 90\nhorizontal_distance_m = sort(rand(n_shots) .* 55.0)\ntrue_a, true_b, true_c = -0.028, 2.0, 0.0\nheight_true_m = true_a .* horizontal_distance_m .^ 2 .+ true_b .* horizontal_distance_m .+ true_c\nheight_m = height_true_m .+ randn(n_shots) .* 1.3\n\n# --- Quadratic least-squares fit (Vandermonde design, degree 2) ----------------\ndegree = 2\nX = hcat(ones(n_shots), horizontal_distance_m, horizontal_distance_m .^ 2)\ncoeffs = X \\ height_m\nfitted_m = X * coeffs\nresiduals = height_m .- fitted_m\nsse = sum(residuals .^ 2)\nsst = sum((height_m .- mean(height_m)) .^ 2)\nr_squared = 1 - sse / sst\nmse = sse / (n_shots - (degree + 1))\nxtx = X' * X\n\nx_grid = range(minimum(horizontal_distance_m), maximum(horizontal_distance_m), length = 200)\nX_grid = hcat(ones(length(x_grid)), collect(x_grid), collect(x_grid) .^ 2)\ny_grid = X_grid * coeffs\nse_grid = [sqrt(mse * (X_grid[i, :]' * (xtx \\ X_grid[i, :]))) for i in 1:size(X_grid, 1)]\ny_lower = y_grid .- 1.96 .* se_grid\ny_upper = y_grid .+ 1.96 .* se_grid\n\n# --- Equation + goodness-of-fit annotation text --------------------------------\nb_sign = coeffs[2] >= 0 ? \"+\" : \"-\"\nc_sign = coeffs[1] >= 0 ? \"+\" : \"-\"\neqn_text = \"ŷ = $(round(coeffs[3], digits=4))x² $(b_sign) $(abs(round(coeffs[2], digits=3)))x $(c_sign) $(abs(round(coeffs[1], digits=2)))\\nR² = $(round(r_squared, digits=3))\"\n\n# --- Plot -----------------------------------------------------------------------\ntitle_str = \"Projectile Motion · scatter-regression-polynomial · julia · makie · anyplot.ai\"\ntitle_fontsize = max(18, round(Int, 32 * 67 / length(title_str)))\n\nfig = Figure(\n    resolution = (1600, 900),\n    fontsize = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title = title_str,\n    titlesize = title_fontsize,\n    titlecolor = INK,\n    xlabel = \"Horizontal Distance (m)\",\n    ylabel = \"Height (m)\",\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)\n\nband!(ax, x_grid, y_lower, y_upper;\n      color = RGBAf(FIT_COLOR.r, FIT_COLOR.g, FIT_COLOR.b, 0.15),\n      label = \"95% confidence band\")\nscatter!(ax, horizontal_distance_m, height_m;\n         color = BRAND, alpha = 0.65, markersize = 11,\n         strokecolor = PAGE_BG, strokewidth = 0.5,\n         label = \"Measured height\")\nlines!(ax, x_grid, y_grid; color = FIT_COLOR, linewidth = 3.0, label = \"Quadratic fit (degree 2)\")\n\ntext!(ax, 0.97, 0.06, text = eqn_text, space = :relative,\n      align = (:right, :bottom), fontsize = 13, color = INK)\n\naxislegend(ax, position = :lt, framevisible = false, labelsize = 12, labelcolor = INK_SOFT)\n\n# --- Save -------------------------------------------------------------------\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}