{"spec_id":"coefficient-confidence","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\ncoefficient-confidence: Coefficient Plot with Confidence Intervals\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 84/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    element_line,\n    element_rect,\n    element_text,\n    geom_errorbarh,\n    geom_point,\n    geom_vline,\n    ggplot,\n    labs,\n    scale_color_manual,\n    theme,\n    theme_minimal,\n)\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# Okabe-Ito palette\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"]\n\n# Data: Coefficients from a housing price regression model\nnp.random.seed(42)\ndata = {\n    \"variable\": [\n        \"Living Area (sq ft)\",\n        \"Bedrooms\",\n        \"Bathrooms\",\n        \"Garage Capacity\",\n        \"Lot Size (acres)\",\n        \"Age (years)\",\n        \"Distance to City (mi)\",\n        \"School Rating\",\n        \"Crime Index\",\n        \"Pool\",\n        \"Central AC\",\n        \"Renovated\",\n    ],\n    \"coefficient\": [0.42, 0.15, 0.28, 0.18, 0.08, -0.22, -0.14, 0.25, -0.31, 0.12, 0.09, 0.06],\n    \"ci_lower\": [0.35, 0.02, 0.18, 0.08, -0.02, -0.30, -0.22, 0.15, -0.42, 0.01, 0.02, -0.04],\n    \"ci_upper\": [0.49, 0.28, 0.38, 0.28, 0.18, -0.14, -0.06, 0.35, -0.20, 0.23, 0.16, 0.16],\n}\n\ndf = pd.DataFrame(data)\n\n# Determine significance (CI does not cross zero)\ndf[\"significant\"] = ~((df[\"ci_lower\"] <= 0) & (df[\"ci_upper\"] >= 0))\ndf[\"significance\"] = df[\"significant\"].map({True: \"Significant\", False: \"Not Significant\"})\n\n# Order variables by coefficient magnitude for readability\ndf[\"variable\"] = pd.Categorical(\n    df[\"variable\"], categories=df.sort_values(\"coefficient\")[\"variable\"].tolist(), ordered=True\n)\n\n# Theme-adaptive colors for significance\nsig_colors = {\n    \"Significant\": IMPRINT[0],  # Brand green\n    \"Not Significant\": INK_SOFT,  # Theme-adaptive soft ink\n}\n\n# Create plot\nplot = (\n    ggplot(df, aes(x=\"coefficient\", y=\"variable\", color=\"significance\"))\n    + geom_vline(xintercept=0, linetype=\"dashed\", color=INK_SOFT, size=0.8, alpha=0.6)\n    + geom_errorbarh(aes(xmin=\"ci_lower\", xmax=\"ci_upper\"), height=0.3, size=1.2)\n    + geom_point(size=5)\n    + scale_color_manual(values=sig_colors)\n    + labs(\n        x=\"Coefficient Estimate (Standardized)\",\n        y=\"Predictor Variable\",\n        title=\"coefficient-confidence · python · plotnine · anyplot.ai\",\n        color=\"Statistical Significance\",\n    )\n    + theme_minimal()\n    + theme(\n        figure_size=(16, 9),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major_y=element_line(color=INK, size=0.3, alpha=0.10),\n        panel_grid_major_x=element_line(color=INK, size=0.3, alpha=0.10),\n        panel_border=element_rect(color=INK_SOFT, fill=None),\n        axis_title=element_text(size=20, color=INK),\n        axis_text=element_text(size=16, color=INK_SOFT),\n        axis_text_y=element_text(size=14, color=INK_SOFT),\n        axis_line=element_line(color=INK_SOFT, size=0.4),\n        plot_title=element_text(size=24, color=INK),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n        legend_text=element_text(size=16, color=INK_SOFT),\n        legend_title=element_text(size=18, color=INK),\n        legend_position=\"right\",\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=300)\n"}