{"spec_id":"scatter-regression-lowess","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nscatter-regression-lowess: Scatter Plot with LOWESS Regression\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 97/100 | Updated: 2026-05-14\n\"\"\"\n\nimport os\nimport sys\n\nimport numpy as np\nimport pandas as pd\n\n\nvenv_path = \"/home/runner/work/anyplot/anyplot/.venv/lib/python3.13/site-packages\"\nif os.path.exists(venv_path):\n    sys.path.insert(0, venv_path)\n\nfrom plotnine import (\n    aes,\n    element_line,\n    element_rect,\n    element_text,\n    geom_point,\n    geom_smooth,\n    ggplot,\n    labs,\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\"\nBRAND = \"#009E73\"\nACCENT = \"#C475FD\"\n\n# Data - create complex non-linear relationship (crop yield vs temperature)\nnp.random.seed(42)\nn_points = 150\n\n# Temperature range (x) - realistic agricultural context\nx = np.linspace(5, 35, n_points)\n\n# Yield (y) - peaks around 20-25°C, drops at extremes (realistic crop response)\n# Complex non-linear pattern: quadratic-like with some local variation\ny_base = -0.5 * (x - 22) ** 2 + 80  # Peak around 22°C\ny_noise = np.random.normal(0, 8, n_points)  # Natural variation\ny = y_base + y_noise + 3 * np.sin(x / 3)  # Add subtle local pattern\n\n# Ensure positive yields\ny = np.clip(y, 5, None)\n\n# Create DataFrame\ndf = pd.DataFrame({\"temperature\": x, \"yield\": y})\n\n# Theme configuration\nanyplot_theme = theme(\n    figure_size=(16, 9),\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_rect(color=INK_SOFT, fill=None),\n    axis_title=element_text(color=INK, size=20),\n    axis_text=element_text(color=INK_SOFT, size=16),\n    axis_line=element_line(color=INK_SOFT),\n    plot_title=element_text(color=INK, size=24),\n    text=element_text(size=14),\n)\n\n# Create plot with scatter points and LOWESS smooth\nplot = (\n    ggplot(df, aes(x=\"temperature\", y=\"yield\"))\n    + geom_point(color=BRAND, alpha=0.6, size=3)\n    + geom_smooth(method=\"lowess\", span=0.4, color=ACCENT, size=2.5, se=False)\n    + labs(\n        x=\"Temperature (°C)\", y=\"Crop Yield (tons/hectare)\", title=\"scatter-regression-lowess · plotnine · anyplot.ai\"\n    )\n    + theme_minimal()\n    + anyplot_theme\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=300)\n"}