{"spec_id":"coefficient-confidence","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\ncoefficient-confidence: Coefficient Plot with Confidence Intervals\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 84/100 | Updated: 2026-05-18\n\"\"\"\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.lines import Line2D\n\n\n# Data: Housing price regression coefficients (standardized)\nnp.random.seed(42)\n\nvariables = [\n    \"Square Footage\",\n    \"Number of Bedrooms\",\n    \"Number of Bathrooms\",\n    \"Lot Size (acres)\",\n    \"Year Built\",\n    \"Distance to City Center\",\n    \"School Rating\",\n    \"Crime Rate Index\",\n    \"Garage Spaces\",\n    \"Has Pool\",\n]\n\n# Coefficients with varying significance and direction\ncoefficients = np.array([0.45, 0.12, 0.28, 0.18, 0.08, -0.32, 0.25, -0.15, 0.10, 0.05])\n# Standard errors for confidence intervals\nstd_errors = np.array([0.08, 0.09, 0.07, 0.06, 0.05, 0.10, 0.06, 0.08, 0.07, 0.04])\n\n# 95% confidence intervals\nci_lower = coefficients - 1.96 * std_errors\nci_upper = coefficients + 1.96 * std_errors\n\n# Determine significance (CI doesn't cross zero)\nsignificant = (ci_lower > 0) | (ci_upper < 0)\n\n# Sort by coefficient magnitude for better readability\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# Calculate error bar lengths\nxerr_lower = coefficients - ci_lower\nxerr_upper = ci_upper - coefficients\nxerr = np.array([xerr_lower, xerr_upper])\n\n# Create plot\nfig, ax = plt.subplots(figsize=(16, 9))\n\n# Y positions for variables\ny_pos = np.arange(len(variables))\n\n# Plot points and error bars with different colors for significant vs non-significant\ncolors = [\"#306998\" if sig else \"#999999\" for sig in significant]\nmarkers = [\"o\" if sig else \"s\" for sig in significant]\n\n# Plot error bars first\nfor i, (coef, y, color, marker) in enumerate(zip(coefficients, y_pos, colors, markers, strict=True)):\n    ax.errorbar(\n        coef,\n        y,\n        xerr=[[xerr_lower[i]], [xerr_upper[i]]],\n        fmt=marker,\n        color=color,\n        markersize=14,\n        markeredgewidth=2,\n        markeredgecolor=\"white\",\n        capsize=8,\n        capthick=3,\n        elinewidth=3,\n        zorder=3,\n    )\n\n# Vertical reference line at zero\nax.axvline(x=0, color=\"#FFD43B\", linewidth=3, linestyle=\"-\", zorder=2, alpha=0.8)\n\n# Labels and styling\nax.set_yticks(y_pos)\nax.set_yticklabels(variables, fontsize=18)\nax.set_xlabel(\"Coefficient Estimate (Standardized)\", fontsize=20)\nax.set_title(\"coefficient-confidence · matplotlib · pyplots.ai\", fontsize=24)\nax.tick_params(axis=\"x\", labelsize=16)\n\n# Grid\nax.grid(True, alpha=0.3, linestyle=\"--\", axis=\"x\")\nax.set_axisbelow(True)\n\n# Legend\nlegend_elements = [\n    Line2D(\n        [0],\n        [0],\n        marker=\"o\",\n        color=\"w\",\n        markerfacecolor=\"#306998\",\n        markersize=14,\n        markeredgewidth=2,\n        markeredgecolor=\"white\",\n        label=\"Significant (p < 0.05)\",\n    ),\n    Line2D(\n        [0],\n        [0],\n        marker=\"s\",\n        color=\"w\",\n        markerfacecolor=\"#999999\",\n        markersize=14,\n        markeredgewidth=2,\n        markeredgecolor=\"white\",\n        label=\"Not Significant\",\n    ),\n]\nax.legend(handles=legend_elements, loc=\"lower right\", fontsize=16, framealpha=0.9)\n\n# Adjust layout\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\n\nplt.tight_layout()\nplt.savefig(\"plot.png\", dpi=300, bbox_inches=\"tight\")\n"}