{"spec_id":"residual-plot","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nresidual-plot: Residual Plot\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 98/100 | Updated: 2026-05-10\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 Band, ColumnDataSource, Label, Span\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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 (residuals)\nOUTLIER_COLOR = \"#C475FD\"  # Okabe-Ito position 2 (outliers)\n\n# Data - Linear regression example with realistic housing price prediction\nnp.random.seed(42)\nn_points = 150\n\n# Generate fitted values (predicted house prices in $1000s)\ny_pred = np.linspace(150, 450, n_points) + np.random.randn(n_points) * 20\n\n# Generate residuals with some heteroscedasticity pattern for demonstration\nbase_residuals = np.random.randn(n_points) * 25\n# Add a few outliers beyond 2 standard deviations\noutlier_indices = [10, 45, 89, 120, 140]\nbase_residuals[outlier_indices] = np.array([62, -68, 58, -65, 70])\n\nresiduals = base_residuals\ny_true = y_pred + residuals\n\n# Calculate statistics for reference bands\nresidual_std = np.std(residuals)\nupper_band = 2 * residual_std\nlower_band = -2 * residual_std\n\n# Identify outliers (beyond ±2 standard deviations)\nis_outlier = np.abs(residuals) > 2 * residual_std\n\n# Prepare data sources\nsource_normal = ColumnDataSource(data={\"x\": y_pred[~is_outlier], \"y\": residuals[~is_outlier]})\nsource_outliers = ColumnDataSource(data={\"x\": y_pred[is_outlier], \"y\": residuals[is_outlier]})\n\n# Band data for ±2 SD region\nband_source = ColumnDataSource(\n    data={\n        \"x\": np.array([min(y_pred) - 10, max(y_pred) + 10]),\n        \"lower\": np.array([lower_band, lower_band]),\n        \"upper\": np.array([upper_band, upper_band]),\n    }\n)\n\n# Create figure (4800x2700 for 16:9)\np = figure(\n    width=4800,\n    height=2700,\n    title=\"residual-plot · bokeh · anyplot.ai\",\n    x_axis_label=\"Fitted Values (Predicted Price in $1000s)\",\n    y_axis_label=\"Residuals (Observed - Predicted)\",\n    tools=\"pan,wheel_zoom,box_zoom,reset,save\",\n    x_range=(min(y_pred) - 20, max(y_pred) + 20),\n    y_range=(min(residuals) - 20, max(residuals) + 20),\n)\n\n# Add ±2 SD band (light fill with theme-adaptive color)\nband = Band(\n    base=\"x\", lower=\"lower\", upper=\"upper\", source=band_source, fill_alpha=0.15, fill_color=INK_SOFT, line_width=0\n)\np.add_layout(band)\n\n# Add horizontal reference line at y=0\nzero_line = Span(location=0, dimension=\"width\", line_color=BRAND, line_width=4, line_dash=\"solid\")\np.add_layout(zero_line)\n\n# Add dashed lines at ±2 SD boundaries\nupper_line = Span(location=upper_band, dimension=\"width\", line_color=INK_SOFT, line_width=2, line_dash=\"dashed\")\nlower_line = Span(location=lower_band, dimension=\"width\", line_color=INK_SOFT, line_width=2, line_dash=\"dashed\")\np.add_layout(upper_line)\np.add_layout(lower_line)\n\n# Plot normal points (Brand color)\np.scatter(\"x\", \"y\", source=source_normal, size=18, color=BRAND, alpha=0.7, legend_label=\"Residuals\")\n\n# Plot outliers (Okabe-Ito color 2)\np.scatter(\n    \"x\",\n    \"y\",\n    source=source_outliers,\n    size=22,\n    color=OUTLIER_COLOR,\n    alpha=0.9,\n    line_color=BRAND,\n    line_width=2,\n    legend_label=\"Outliers (>2 SD)\",\n)\n\n# Add labels for ±2 SD bands (larger font for better visibility)\nlabel_upper = Label(\n    x=min(y_pred) + 10,\n    y=upper_band + 3,\n    text=\"+2 SD\",\n    text_font_size=\"24pt\",\n    text_color=INK_SOFT,\n    text_font_style=\"italic\",\n)\nlabel_lower = Label(\n    x=min(y_pred) + 10,\n    y=lower_band + 3,\n    text=\"-2 SD\",\n    text_font_size=\"24pt\",\n    text_color=INK_SOFT,\n    text_font_style=\"italic\",\n)\np.add_layout(label_upper)\np.add_layout(label_lower)\n\n# Styling - Text sizes 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.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\n\n# Grid styling\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# Legend styling\np.legend.location = \"top_left\"\np.legend.label_text_font_size = \"20pt\"\np.legend.label_text_color = INK_SOFT\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\np.legend.padding = 15\np.legend.spacing = 10\n\n# Axis styling\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\n# Background\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Save HTML output\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome (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"}