{"spec_id":"scatter-regression-linear","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nscatter-regression-linear: Scatter Plot with Linear Regression\nLibrary: letsplot 4.11.0 | Python 3.13.14\nQuality: 85/100 | Updated: 2026-08-05\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\nfrom lets_plot.export import ggsave as export_ggsave\n\n\nLetsPlot.setup_html()\n\n# Theme tokens (Imprint)\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nGRID_COLOR = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\nBRAND = \"#009E73\"  # Imprint palette position 1\nSECONDARY = \"#C475FD\"  # Imprint palette position 2 — regression line + CI band\n\n# Data - Advertising spend vs sales revenue relationship\nnp.random.seed(42)\nn = 100\nadvertising_spend = np.random.uniform(10, 100, n)\nsales_revenue = 2.5 * advertising_spend + 50 + np.random.randn(n) * 25\n\ndf = pd.DataFrame({\"advertising_spend\": advertising_spend, \"sales_revenue\": sales_revenue})\n\n# Regression statistics (closed-form OLS, so the annotation matches geom_smooth exactly)\nx_mean = np.mean(advertising_spend)\ny_mean = np.mean(sales_revenue)\nslope = np.sum((advertising_spend - x_mean) * (sales_revenue - y_mean)) / np.sum((advertising_spend - x_mean) ** 2)\nintercept = y_mean - slope * x_mean\n\ny_pred = slope * advertising_spend + intercept\nss_res = np.sum((sales_revenue - y_pred) ** 2)\nss_tot = np.sum((sales_revenue - y_mean) ** 2)\nr_squared = 1 - (ss_res / ss_tot)\n\nequation_text = f\"y = {slope:.2f}x + {intercept:.1f}\"\nr2_text = f\"R² = {r_squared:.3f}\"\nannotation_text = f\"{equation_text}\\n{r2_text}\"\n\nannotation_x = 15\nannotation_y = sales_revenue.max() - 8\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"advertising_spend\", y=\"sales_revenue\"))\n    + geom_point(\n        shape=21,\n        fill=BRAND,\n        color=PAGE_BG,\n        stroke=0.6,\n        size=3,\n        alpha=0.65,\n        tooltips=layer_tooltips()\n        .line(\"Ad Spend|$@advertising_spend{.1f}K\")\n        .line(\"Sales|$@sales_revenue{.1f}K\"),\n    )\n    + geom_smooth(\n        method=\"lm\",\n        color=SECONDARY,\n        size=1.6,\n        se=True,\n        level=0.95,\n        fill=SECONDARY,\n        fill_alpha=0.15,\n        tooltips=layer_tooltips()\n        .line(\"Fitted|@..y..\")\n        .line(\"95% CI|[@..ymin.., @..ymax..]\"),\n    )\n    + geom_label(\n        x=annotation_x,\n        y=annotation_y,\n        label=annotation_text,\n        size=4.5,\n        color=INK,\n        fill=ELEVATED_BG,\n        label_size=0,\n        family=\"sans-serif\",\n        hjust=0,\n        vjust=1,\n        alpha=0.92,\n    )\n    + labs(\n        x=\"Advertising Spend ($K)\", y=\"Sales Revenue ($K)\", title=\"scatter-regression-linear · letsplot · anyplot.ai\"\n    )\n    + scale_x_continuous(expand=(0.03, 0))\n    + scale_y_continuous(expand=(0.05, 0))\n    + ggmarginal(\n        \"tr\",\n        size=0.09,\n        layer=geom_density(color=BRAND, fill=BRAND, alpha=0.25, size=0.8),\n    )\n    + ggsize(800, 450)\n    + theme_minimal()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_border=element_blank(),\n        panel_grid_major=element_line(color=GRID_COLOR, size=0.6),\n        panel_grid_minor=element_blank(),\n        axis_text=element_text(size=10, color=INK_SOFT),\n        axis_title=element_text(size=12, color=INK),\n        plot_title=element_text(size=16, color=INK),\n        axis_line_x=element_line(color=INK_SOFT, size=0.6),\n        axis_line_y=element_line(color=INK_SOFT, size=0.6),\n        axis_ticks=element_blank(),\n    )\n)\n\n# Save outputs\nexport_ggsave(plot, filename=f\"plot-{THEME}.png\", path=\".\", scale=4)\nexport_ggsave(plot, filename=f\"plot-{THEME}.html\", path=\".\")\n"}