{"spec_id":"scatter-regression-lowess","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nscatter-regression-lowess: Scatter Plot with LOWESS Regression\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-14\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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\"\n\nBRAND = \"#009E73\"  # Okabe-Ito position 1\nACCENT = \"#C475FD\"  # Okabe-Ito position 2 for LOWESS curve\n\n# Data: Simulate a complex non-linear relationship (e.g., temperature vs enzyme activity)\nnp.random.seed(42)\nn = 200\n\n# Create x values (temperature in Celsius)\nx = np.linspace(10, 50, n) + np.random.normal(0, 1, n)\nx = np.sort(x)\n\n# Create y with complex non-linear relationship (enzyme activity %)\n# Activity increases, peaks around 35°C, then decreases (typical enzyme behavior)\ny_true = 20 + 60 * np.exp(-0.5 * ((x - 35) / 8) ** 2)\ny = y_true + np.random.normal(0, 5, n)\n\n# Calculate LOWESS regression\nlowess_result = lowess(y, x, frac=0.4)\nx_lowess = lowess_result[:, 0]\ny_lowess = lowess_result[:, 1]\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"scatter-regression-lowess · bokeh · anyplot.ai\",\n    x_axis_label=\"Temperature (°C)\",\n    y_axis_label=\"Enzyme Activity (%)\",\n)\n\n# Scatter points with HoverTool\nsource_scatter = ColumnDataSource(data={\"x\": x, \"y\": y})\nscatter = p.scatter(x=\"x\", y=\"y\", source=source_scatter, size=18, color=BRAND, alpha=0.6, legend_label=\"Data Points\")\n\n# Add hover tool for interactivity\nhover = HoverTool(tooltips=[(\"Temperature\", \"@x{0.0}°C\"), (\"Activity\", \"@y{0.0}%\")], renderers=[scatter])\np.add_tools(hover)\n\n# LOWESS curve\nsource_lowess = ColumnDataSource(data={\"x\": x_lowess, \"y\": y_lowess})\np.line(x=\"x\", y=\"y\", source=source_lowess, line_width=5, color=ACCENT, legend_label=\"LOWESS Fit\")\n\n# Styling - larger text for 4800x2700 canvas\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\n# Grid styling - solid, subtle\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\n\n# Background and border colors\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Axis styling\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\n# Legend styling - increased size for canvas scale\np.legend.label_text_font_size = \"18pt\"\np.legend.label_text_color = INK_SOFT\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\np.legend.location = \"top_right\"\n\n# Save HTML output\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome using Selenium\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}