{"spec_id":"coefficient-confidence","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\ncoefficient-confidence: Coefficient Plot with Confidence Intervals\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 87/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\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\nSIGNIFICANT_COLOR = \"#009E73\"  # Okabe-Ito position 1 — brand green\nNEUTRAL_COLOR = INK_SOFT  # Adaptive neutral for non-significant\n\n# Data: Regression coefficients from a 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 Rating\",\n    \"Crime Rate Index\",\n    \"Garage Spaces\",\n    \"Has Pool\",\n    \"Renovated Recently\",\n    \"Neighborhood Tier\",\n]\n\n# Generate realistic regression coefficients\ncoefficients = [0.45, 0.12, 0.18, 0.08, -0.15, -0.22, 0.25, -0.31, 0.09, 0.14, 0.11, 0.28]\n\n# Generate confidence intervals (wider for less certain estimates)\nci_widths = [0.08, 0.15, 0.12, 0.18, 0.09, 0.14, 0.11, 0.16, 0.20, 0.13, 0.22, 0.10]\nci_lower = [c - w for c, w in zip(coefficients, ci_widths, strict=True)]\nci_upper = [c + w for c, w in zip(coefficients, ci_widths, strict=True)]\n\n# Determine significance (CI does not cross zero)\nsignificant = [not (low <= 0 <= high) for low, high in zip(ci_lower, ci_upper, strict=True)]\n\n# Create DataFrame\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 easier comparison\ndf = df.sort_values(\"coefficient\", ascending=True).reset_index(drop=True)\n\n# Configure seaborn theme\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Create figure\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Assign colors based on significance\ncolors = [SIGNIFICANT_COLOR if sig else NEUTRAL_COLOR for sig in df[\"significant\"]]\n\n# Plot error bars (confidence intervals)\ny_positions = np.arange(len(df))\nfor i, row in df.iterrows():\n    color = SIGNIFICANT_COLOR if row[\"significant\"] else NEUTRAL_COLOR\n    ax.hlines(y=i, xmin=row[\"ci_lower\"], xmax=row[\"ci_upper\"], color=color, linewidth=3, alpha=0.7)\n\n# Plot coefficient points\nscatter_df = df.copy()\nscatter_df[\"y_pos\"] = y_positions\n\nsns.scatterplot(\n    data=scatter_df,\n    x=\"coefficient\",\n    y=\"y_pos\",\n    hue=\"significant\",\n    palette={True: SIGNIFICANT_COLOR, False: NEUTRAL_COLOR},\n    s=400,\n    ax=ax,\n    legend=True,\n    zorder=5,\n)\n\n# Add vertical reference line at zero\nax.axvline(x=0, color=INK_SOFT, linewidth=2, linestyle=\"--\", alpha=0.5, zorder=1)\n\n# Set y-axis labels to variable names\nax.set_yticks(y_positions)\nax.set_yticklabels(df[\"variable\"], fontsize=16, color=INK)\n\n# Styling\nax.set_xlabel(\"Coefficient Estimate (Standardized)\", fontsize=20, color=INK)\nax.set_ylabel(\"Predictor Variable\", fontsize=20, color=INK)\nax.set_title(\"coefficient-confidence · python · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"x\", labelsize=16, colors=INK_SOFT)\nax.grid(True, axis=\"x\", alpha=0.10, linewidth=0.8, color=INK)\n\n# Update legend with correct label order\nhandles, labels = ax.get_legend_handles_labels()\nax.legend(handles, [\"Not Significant\", \"Significant (p < 0.05)\"], fontsize=14, loc=\"lower right\", framealpha=0.95)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}