{"spec_id":"coefficient-confidence","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\ncoefficient-confidence: Coefficient Plot with Confidence Intervals\nLibrary: letsplot 4.9.0 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\n\n\nLetsPlot.setup_html()\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 — first series (significant)\nNEUTRAL = \"#1A1A1A\" if THEME == \"light\" else \"#E8E8E0\"  # Non-significant\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    \"Lot Size (acres)\",\n    \"Age of Home (years)\",\n    \"Distance to Downtown\",\n    \"School District Rating\",\n    \"Garage Spaces\",\n    \"Has Pool\",\n    \"Has Fireplace\",\n    \"Renovated Recently\",\n    \"Crime Rate Index\",\n]\n\n# Generate realistic coefficients - some significant, some not\ncoefficients = [0.45, 0.12, 0.28, 0.18, -0.15, -0.22, 0.35, 0.08, 0.14, 0.05, 0.20, -0.32]\nstd_errors = [0.08, 0.09, 0.07, 0.06, 0.04, 0.05, 0.06, 0.07, 0.10, 0.08, 0.09, 0.07]\n\n# Calculate confidence intervals (95%)\nci_lower = [c - 1.96 * se for c, se in zip(coefficients, std_errors, strict=False)]\nci_upper = [c + 1.96 * se for c, se in zip(coefficients, std_errors, strict=False)]\n\n# Determine significance (CI does not cross zero)\nsignificant = [(ci_low > 0 or ci_hi < 0) for ci_low, ci_hi in zip(ci_lower, ci_upper, strict=False)]\n\ndf = pd.DataFrame(\n    {\n        \"variable\": variables,\n        \"coefficient\": coefficients,\n        \"ci_lower\": ci_lower,\n        \"ci_upper\": ci_upper,\n        \"significant\": significant,\n    }\n)\n\n# Sort by coefficient magnitude for better visualization\ndf = df.sort_values(\"coefficient\", ascending=True).reset_index(drop=True)\n\n# Create ordered categorical for proper y-axis ordering\ndf[\"variable\"] = pd.Categorical(df[\"variable\"], categories=df[\"variable\"].tolist(), ordered=True)\n\n# Add significance label for coloring\ndf[\"sig_label\"] = df[\"significant\"].map({True: \"Significant (p < 0.05)\", False: \"Not Significant\"})\ndf[\"sig_label\"] = pd.Categorical(\n    df[\"sig_label\"], categories=[\"Significant (p < 0.05)\", \"Not Significant\"], ordered=True\n)\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"variable\", y=\"coefficient\"))\n    # Horizontal reference line at zero\n    + geom_hline(yintercept=0, color=INK_SOFT, size=0.8, linetype=\"dashed\")\n    # Point range for coefficient with confidence intervals\n    + geom_pointrange(aes(ymin=\"ci_lower\", ymax=\"ci_upper\", color=\"sig_label\"), size=1.5, fatten=4)\n    # Flip coordinates for horizontal layout\n    + coord_flip()\n    # Color scale - Okabe-Ito for significant, neutral for non-significant\n    + scale_color_manual(values=[BRAND, NEUTRAL], name=\"Statistical Significance\")\n    # Labels\n    + labs(\n        y=\"Coefficient Estimate\",\n        x=\"Predictor Variable\",\n        title=\"coefficient-confidence · python · letsplot · anyplot.ai\",\n    )\n    # Theme\n    + theme_minimal()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major_x=element_line(color=INK_SOFT, size=0.3),\n        panel_grid_minor_x=element_blank(),\n        panel_grid_major_y=element_blank(),\n        panel_grid_minor_y=element_blank(),\n        axis_line=element_line(color=INK_SOFT, size=0.5),\n        plot_title=element_text(size=24, face=\"bold\", color=INK),\n        axis_title_x=element_text(size=20, color=INK),\n        axis_title_y=element_text(size=20, color=INK),\n        axis_text_x=element_text(size=16, color=INK_SOFT),\n        axis_text_y=element_text(size=16, color=INK_SOFT),\n        legend_title=element_text(size=16, color=INK),\n        legend_text=element_text(size=16, color=INK_SOFT),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n        legend_position=\"bottom\",\n    )\n    # Size\n    + ggsize(1600, 900)\n)\n\n# Save as PNG (scale 3x for 4800 x 2700)\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=3)\n\n# Save interactive HTML\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}