{"spec_id":"scatter-regression-lowess","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nscatter-regression-lowess: Scatter Plot with LOWESS Regression\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 84/100 | Updated: 2026-05-14\n\"\"\"\n\nimport os\nimport numpy as np\nimport plotly.graph_objects as go\nfrom statsmodels.nonparametric.smoothers_lowess import lowess\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\"\nGRID        = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nBRAND       = \"#009E73\"  # Okabe-Ito position 1\nACCENT      = \"#C475FD\"  # Okabe-Ito position 2\n\n# Data - Enzyme kinetics: realistic biological dose-response relationship\n# Different structure than seaborn: exponential saturation curve with noise\nnp.random.seed(42)\nn_points = 150\n# Substrate concentration (biological domain)\nx = np.linspace(0.1, 50, n_points)\n# Michaelis-Menten style response: complex saturation with local variations\ny = (100 * x) / (10 + x) + np.random.normal(0, 3, n_points) + 5 * np.sin(x / 5)\n\n# Apply LOWESS smoothing using statsmodels\nlowess_result = lowess(y, x, frac=0.35, it=3)\nx_lowess = lowess_result[:, 0]\ny_lowess = lowess_result[:, 1]\n\n# Create figure\nfig = go.Figure()\n\n# Add scatter points\nfig.add_trace(\n    go.Scatter(\n        x=x,\n        y=y,\n        mode=\"markers\",\n        name=\"Measured Values\",\n        marker=dict(\n            size=11,\n            color=BRAND,\n            opacity=0.6,\n            line=dict(color=PAGE_BG, width=0.5),\n        ),\n    )\n)\n\n# Add LOWESS curve\nfig.add_trace(\n    go.Scatter(\n        x=x_lowess,\n        y=y_lowess,\n        mode=\"lines\",\n        name=\"LOWESS Smooth\",\n        line=dict(\n            color=ACCENT,\n            width=4,\n        ),\n    )\n)\n\n# Update layout for large canvas with theme-adaptive styling\nfig.update_layout(\n    title=dict(\n        text=\"scatter-regression-lowess · plotly · anyplot.ai\",\n        font=dict(size=28, color=INK),\n        x=0.5,\n        xanchor=\"center\",\n    ),\n    xaxis=dict(\n        title=dict(text=\"Substrate Concentration (µM)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Enzyme Activity (V/V_max)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font=dict(color=INK),\n    legend=dict(\n        font=dict(size=18, color=INK_SOFT),\n        x=0.02,\n        y=0.98,\n        xanchor=\"left\",\n        yanchor=\"top\",\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n    ),\n    margin=dict(l=100, r=60, t=100, b=100),\n    showlegend=True,\n)\n\n# Save as PNG (4800 x 2700 px)\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\n\n# Save as HTML for interactivity\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}