{"spec_id":"coefficient-confidence","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\ncoefficient-confidence: Coefficient Plot with Confidence Intervals\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-18\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, Legend, LegendItem, 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\n# Data - Regression coefficients for housing price prediction model\nnp.random.seed(42)\n\nvariables = [\n    \"Square Footage\",\n    \"Number of Bedrooms\",\n    \"Number of Bathrooms\",\n    \"Age of House\",\n    \"Distance to City Center\",\n    \"Lot Size\",\n    \"Garage Size\",\n    \"School Rating\",\n    \"Crime Rate Index\",\n    \"Property Tax Rate\",\n]\n\n# Generate realistic regression coefficients (some significant, some not)\ncoefficients = np.array([0.45, 0.12, 0.18, -0.08, -0.22, 0.15, 0.09, 0.28, -0.35, -0.05])\nstd_errors = np.array([0.05, 0.08, 0.06, 0.03, 0.07, 0.04, 0.06, 0.05, 0.09, 0.07])\n\n# Calculate 95% confidence intervals\nci_lower = coefficients - 1.96 * std_errors\nci_upper = coefficients + 1.96 * std_errors\n\n# Determine significance (CI does not cross zero)\nsignificant = ~((ci_lower < 0) & (ci_upper > 0))\n\n# Sort by coefficient magnitude for better visualization\nsort_idx = np.argsort(np.abs(coefficients))\nvariables = [variables[i] for i in sort_idx]\ncoefficients = coefficients[sort_idx]\nci_lower = ci_lower[sort_idx]\nci_upper = ci_upper[sort_idx]\nsignificant = significant[sort_idx]\n\n# Okabe-Ito palette - using brand green for significant, neutral for non-significant\nSIG_COLOR = \"#009E73\"\nNONSIG_COLOR = INK_SOFT\n\ncolors = [SIG_COLOR if sig else NONSIG_COLOR for sig in significant]\n\n# Create figure with categorical y-axis\np = figure(\n    width=4800,\n    height=2700,\n    y_range=variables,\n    title=\"coefficient-confidence · python · bokeh · anyplot.ai\",\n    x_axis_label=\"Coefficient Estimate (Standardized)\",\n    y_axis_label=\"Predictor Variable\",\n)\n\n# Add vertical reference line at zero\nzero_line = Span(location=0, dimension=\"height\", line_color=INK_SOFT, line_width=3, line_dash=\"dashed\")\np.add_layout(zero_line)\n\n# Draw confidence interval segments (error bars) with distinct colors\nfor i, var in enumerate(variables):\n    color = colors[i]\n    # Main confidence interval line\n    p.line(x=[ci_lower[i], ci_upper[i]], y=[var, var], line_width=6, line_color=color, line_alpha=0.85)\n\n# Plot coefficient points - separate renderers for legend with distinct colors\nsig_indices = [i for i, s in enumerate(significant) if s]\nnonsig_indices = [i for i, s in enumerate(significant) if not s]\n\n# Create separate data sources for legend\nsig_source = ColumnDataSource(\n    data={\n        \"variables\": [variables[i] for i in sig_indices],\n        \"coefficients\": [coefficients[i] for i in sig_indices],\n        \"ci_lower_fmt\": [f\"{ci_lower[i]:.3f}\" for i in sig_indices],\n        \"ci_upper_fmt\": [f\"{ci_upper[i]:.3f}\" for i in sig_indices],\n        \"coef_fmt\": [f\"{coefficients[i]:.3f}\" for i in sig_indices],\n        \"significance\": [\"Significant (p < 0.05)\"] * len(sig_indices),\n    }\n)\n\nnonsig_source = ColumnDataSource(\n    data={\n        \"variables\": [variables[i] for i in nonsig_indices],\n        \"coefficients\": [coefficients[i] for i in nonsig_indices],\n        \"ci_lower_fmt\": [f\"{ci_lower[i]:.3f}\" for i in nonsig_indices],\n        \"ci_upper_fmt\": [f\"{ci_upper[i]:.3f}\" for i in nonsig_indices],\n        \"coef_fmt\": [f\"{coefficients[i]:.3f}\" for i in nonsig_indices],\n        \"significance\": [\"Not Significant\"] * len(nonsig_indices),\n    }\n)\n\n# Render significant points with brand green\nsig_renderer = p.scatter(\n    x=\"coefficients\", y=\"variables\", source=sig_source, size=30, color=SIG_COLOR, line_color=\"white\", line_width=3\n)\n\n# Render non-significant points with muted color\nnonsig_renderer = p.scatter(\n    x=\"coefficients\", y=\"variables\", source=nonsig_source, size=30, color=NONSIG_COLOR, line_color=\"white\", line_width=3\n)\n\n# Add HoverTool for interactive tooltips (Bokeh distinctive feature)\nhover = HoverTool(\n    tooltips=[\n        (\"Variable\", \"@variables\"),\n        (\"Coefficient\", \"@coef_fmt\"),\n        (\"95% CI\", \"[@ci_lower_fmt, @ci_upper_fmt]\"),\n        (\"Status\", \"@significance\"),\n    ],\n    renderers=[sig_renderer, nonsig_renderer],\n)\np.add_tools(hover)\n\n# Create legend inside the plot area (top right corner within plot bounds)\nlegend = Legend(\n    items=[\n        LegendItem(label=\"Significant (p < 0.05)\", renderers=[sig_renderer]),\n        LegendItem(label=\"Not Significant\", renderers=[nonsig_renderer]),\n    ],\n    location=\"top_right\",\n    label_text_font_size=\"24pt\",\n    label_text_color=INK_SOFT,\n    glyph_width=40,\n    glyph_height=40,\n    border_line_color=INK_SOFT,\n    border_line_width=2,\n    background_fill_color=ELEVATED_BG,\n    background_fill_alpha=0.95,\n    padding=20,\n    margin=30,\n)\np.add_layout(legend)\n\n# Style text sizes for large canvas (scaled for 4800x2700)\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\np.xgrid.grid_line_alpha = 0.10\np.xgrid.grid_line_dash = \"dashed\"\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_alpha = 0.10\np.ygrid.grid_line_dash = \"dashed\"\np.ygrid.grid_line_color = INK\n\n# Background styling (theme-adaptive)\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.axis_line_width = 2\np.yaxis.axis_line_width = 2\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\np.xaxis.major_tick_line_width = 2\np.yaxis.major_tick_line_width = 2\n\n# Save plot (HTML and PNG via Selenium)\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome via 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"}