{"spec_id":"scatter-regression-polynomial","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nscatter-regression-polynomial: Scatter Plot with Polynomial Regression\nLibrary: plotnine 0.15.7 | Python 3.13.14\nQuality: 92/100 | Updated: 2026-08-11\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom mizani.formatters import label_dollar\nfrom plotnine import (\n    aes,\n    annotate,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_point,\n    geom_rug,\n    geom_smooth,\n    ggplot,\n    labs,\n    scale_x_continuous,\n    scale_y_continuous,\n    theme,\n    theme_minimal,\n)\n\n\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nBRAND = \"#009E73\"\nTREND = \"#C475FD\"\n\n# Diminishing-returns marketing economics: ad spend growth outpaces revenue\n# growth as budgets scale, a concave (monotonic, saturating) curve rather\n# than the U-shaped minimum used by the temperature/energy sibling scenario.\nnp.random.seed(42)\nn_points = 90\nspend = np.random.uniform(5, 200, n_points)\nrevenue = 20 + 3.2 * spend - 0.0075 * spend**2 + np.random.normal(0, 15, n_points)\n\ndf = pd.DataFrame({\"spend\": spend, \"revenue\": revenue})\n\ncoeffs = np.polyfit(spend, revenue, 2)\npoly_func = np.poly1d(coeffs)\ny_pred = poly_func(spend)\nss_res = np.sum((revenue - y_pred) ** 2)\nss_tot = np.sum((revenue - np.mean(revenue)) ** 2)\nr_squared = 1 - (ss_res / ss_tot)\n\na, b, c = coeffs\nb_sign = \"+\" if b >= 0 else \"-\"\nc_sign = \"+\" if c >= 0 else \"-\"\nequation_text = f\"y = {a:.4f}x² {b_sign} {abs(b):.3f}x {c_sign} {abs(c):.2f}\"\nr_squared_text = f\"R² = {r_squared:.3f}\"\nannotation_text = f\"{equation_text}\\n{r_squared_text}\"\n\nmoney_fmt = label_dollar(prefix=\"$\", suffix=\"K\", precision=0)\n\nplot = (\n    ggplot(df, aes(x=\"spend\", y=\"revenue\"))\n    + geom_point(size=4, alpha=0.65, color=BRAND)\n    + geom_rug(sides=\"b\", alpha=0.35, color=BRAND, length=0.025)\n    + geom_smooth(method=\"lm\", formula=\"y ~ I(x) + I(x**2)\", se=True, color=TREND, fill=INK_MUTED, alpha=0.25, size=2)\n    + annotate(\n        \"label\",\n        x=195,\n        y=55,\n        label=annotation_text,\n        ha=\"right\",\n        va=\"bottom\",\n        size=17,\n        color=INK,\n        fill=ELEVATED_BG,\n        label_size=0.6,\n        label_padding=0.3,\n    )\n    + labs(title=\"scatter-regression-polynomial · plotnine · anyplot.ai\", x=\"Marketing Spend\", y=\"Revenue\")\n    + scale_x_continuous(labels=money_fmt)\n    + scale_y_continuous(labels=money_fmt)\n    + theme_minimal()\n    + theme(\n        figure_size=(8, 4.5),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=INK, size=0.3, alpha=0.10),\n        panel_grid_minor=element_line(color=INK, size=0.2, alpha=0.05),\n        panel_border=element_blank(),\n        text=element_text(size=7, color=INK),\n        axis_title=element_text(size=10, color=INK),\n        axis_text=element_text(size=8, color=INK_SOFT),\n        axis_line=element_line(color=INK_SOFT),\n        plot_title=element_text(size=12, color=INK),\n    )\n)\n\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\", verbose=False)\n"}