{"spec_id":"coefficient-confidence","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\ncoefficient-confidence: Coefficient Plot with Confidence Intervals\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 87/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme-adaptive colors\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\n\n# Data: Housing price regression coefficients\nnp.random.seed(42)\n\nvariables = [\n    \"Square Footage\",\n    \"Bedrooms\",\n    \"Bathrooms\",\n    \"Garage Spaces\",\n    \"Lot Size (acres)\",\n    \"Age (years)\",\n    \"Distance to City Center\",\n    \"School Rating\",\n    \"Crime Rate Index\",\n    \"Property Tax Rate\",\n    \"Has Pool\",\n    \"Has Basement\",\n]\n\n# Generate realistic regression coefficients with varying significance\ncoefficients = np.array([0.45, 0.12, 0.18, 0.08, 0.22, -0.15, -0.28, 0.32, -0.19, -0.05, 0.14, 0.09])\nstandard_errors = np.array([0.08, 0.09, 0.06, 0.05, 0.07, 0.04, 0.10, 0.08, 0.11, 0.06, 0.05, 0.07])\n\n# 95% confidence intervals\nci_lower = coefficients - 1.96 * standard_errors\nci_upper = coefficients + 1.96 * standard_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(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# Colors based on significance\ncolors = [BRAND if sig else INK_SOFT for sig in significant]\nmarker_symbols = [\"circle\" if sig else \"circle-open\" for sig in significant]\n\n# Create figure\nfig = go.Figure()\n\n# Add error bars (confidence intervals)\nfor i in range(len(variables)):\n    # Error bar line\n    fig.add_trace(\n        go.Scatter(\n            x=[ci_lower[i], ci_upper[i]],\n            y=[variables[i], variables[i]],\n            mode=\"lines\",\n            line={\"color\": colors[i], \"width\": 4},\n            showlegend=False,\n            hoverinfo=\"skip\",\n        )\n    )\n\n    # End caps for error bars\n    fig.add_trace(\n        go.Scatter(\n            x=[ci_lower[i], ci_upper[i]],\n            y=[variables[i], variables[i]],\n            mode=\"markers\",\n            marker={\"symbol\": \"line-ns\", \"size\": 16, \"color\": colors[i], \"line\": {\"width\": 3}},\n            showlegend=False,\n            hoverinfo=\"skip\",\n        )\n    )\n\n# Add coefficient points\nfig.add_trace(\n    go.Scatter(\n        x=coefficients[significant],\n        y=[variables[i] for i in range(len(variables)) if significant[i]],\n        mode=\"markers\",\n        marker={\"size\": 18, \"color\": BRAND, \"line\": {\"width\": 2, \"color\": PAGE_BG}},\n        name=\"Significant (p < 0.05)\",\n        hovertemplate=\"%{y}<br>Coefficient: %{x:.3f}<extra></extra>\",\n    )\n)\n\nfig.add_trace(\n    go.Scatter(\n        x=coefficients[~significant],\n        y=[variables[i] for i in range(len(variables)) if not significant[i]],\n        mode=\"markers\",\n        marker={\"size\": 18, \"color\": INK_SOFT, \"symbol\": \"circle-open\", \"line\": {\"width\": 3, \"color\": INK_SOFT}},\n        name=\"Not Significant\",\n        hovertemplate=\"%{y}<br>Coefficient: %{x:.3f}<extra></extra>\",\n    )\n)\n\n# Add vertical reference line at zero\nfig.add_vline(\n    x=0,\n    line={\"color\": INK_SOFT, \"width\": 2, \"dash\": \"dash\"},\n    annotation_text=\"Null\",\n    annotation_position=\"top\",\n    annotation_font={\"size\": 16, \"color\": INK_SOFT},\n)\n\n# Layout\nfig.update_layout(\n    title={\n        \"text\": \"coefficient-confidence · python · plotly · anyplot.ai\",\n        \"font\": {\"size\": 28, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    xaxis={\n        \"title\": {\"text\": \"Coefficient Estimate (Standardized)\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n        \"zeroline\": False,\n        \"gridcolor\": GRID,\n        \"range\": [-0.6, 0.7],\n        \"linecolor\": INK_SOFT,\n        \"zerolinecolor\": INK_SOFT,\n    },\n    yaxis={\n        \"title\": {\"text\": \"Predictor Variable\", \"font\": {\"size\": 22, \"color\": INK}},\n        \"tickfont\": {\"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={\"color\": INK},\n    legend={\n        \"orientation\": \"h\",\n        \"yanchor\": \"bottom\",\n        \"y\": 1.02,\n        \"xanchor\": \"center\",\n        \"x\": 0.5,\n        \"font\": {\"size\": 18, \"color\": INK_SOFT},\n        \"bgcolor\": ELEVATED_BG,\n        \"bordercolor\": INK_SOFT,\n        \"borderwidth\": 1,\n    },\n    margin={\"l\": 200, \"r\": 80, \"t\": 120, \"b\": 80},\n)\n\n# Save as PNG (4800x2700)\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"}