{"spec_id":"scatter-regression-lowess","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nscatter-regression-lowess: Scatter Plot with LOWESS Regression\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-14\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom statsmodels.nonparametric.smoothers_lowess import lowess\n\n\n# Theme tokens\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\"\nBRAND = \"#009E73\"\nACCENT = \"#C475FD\"\n\n# Data - tree height vs age with realistic growth pattern (power law)\nnp.random.seed(42)\nn_points = 150\nage = np.linspace(1, 30, n_points)\n# Realistic growth curve: power law with decreasing growth rate\nheight = 20 * (1 - np.exp(-0.15 * age)) + np.random.normal(0, 0.6, n_points)\n\n# Compute LOWESS smoothed curve\nlowess_result = lowess(height, age, frac=0.4, return_sorted=True)\nage_smooth = lowess_result[:, 0]\nheight_smooth = lowess_result[:, 1]\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Scatter points\nax.scatter(age, height, s=180, alpha=0.6, color=BRAND, edgecolors=PAGE_BG, linewidth=0.8, label=\"Observed heights\")\n\n# LOWESS regression curve\nax.plot(age_smooth, height_smooth, color=ACCENT, linewidth=4.5, label=\"LOWESS smoothed trend\")\n\n# Style\nax.set_xlabel(\"Tree Age (years)\", fontsize=20, color=INK)\nax.set_ylabel(\"Height (meters)\", fontsize=20, color=INK)\nax.set_title(\"scatter-regression-lowess · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\n\nleg = ax.legend(fontsize=16, loc=\"lower right\", frameon=True)\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    leg.get_frame().set_linewidth(0.8)\n    for text in leg.get_texts():\n        text.set_color(INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}