{"spec_id":"coefficient-confidence","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\ncoefficient-confidence: Coefficient Plot with Confidence Intervals\nLibrary: pygal 3.1.0 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pygal\nfrom pygal.style import Style\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Okabe-Ito palette\nBRAND = \"#009E73\"  # Significant coefficients (position 1 - brand green)\nMUTED = INK_MUTED  # Non-significant coefficients (theme-adaptive muted)\n\n# Data: Coefficients from a housing price regression model\nnp.random.seed(42)\n\nvariables = [\n    \"Square Footage\",\n    \"Number of Bedrooms\",\n    \"Number of Bathrooms\",\n    \"Garage Size\",\n    \"Lot Size (acres)\",\n    \"Age of Home (years)\",\n    \"Distance to City Center\",\n    \"School Rating\",\n    \"Crime Rate Index\",\n    \"Property Tax Rate\",\n]\n\n# Generate realistic coefficients\ncoefficients = np.array([0.45, 0.12, 0.28, 0.18, 0.35, -0.22, -0.15, 0.25, -0.08, -0.05])\nstd_errors = np.array([0.08, 0.09, 0.06, 0.05, 0.10, 0.07, 0.12, 0.08, 0.11, 0.09])\n\n# Calculate 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 easier comparison\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\nn_vars = len(variables)\n\n# Custom style for theme-adaptive rendering\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    colors=(BRAND, MUTED),\n    title_font_size=28,\n    label_font_size=18,\n    major_label_font_size=16,\n    legend_font_size=16,\n    value_font_size=14,\n    stroke_width=3,\n)\n\n# Create XY chart for coefficient plot\nchart = pygal.XY(\n    width=4800,\n    height=2700,\n    style=custom_style,\n    title=\"coefficient-confidence · python · pygal · anyplot.ai\",\n    x_title=\"Coefficient Estimate\",\n    show_legend=True,\n    legend_at_bottom=True,\n    legend_at_bottom_columns=2,\n    show_y_guides=False,\n    show_x_guides=True,\n    dots_size=18,\n    stroke=False,\n    xrange=(-0.6, 0.7),\n    range=(0, n_vars + 1),\n    margin_top=80,\n    margin_bottom=100,\n    margin_left=300,\n    margin_right=80,\n    spacing=30,\n    y_labels=[{\"value\": i + 1, \"label\": variables[i]} for i in range(n_vars)],\n)\n\n# Build data series\nsig_points = []\nnonsig_points = []\nci_sig = []\nci_nonsig = []\n\nfor i, (coef, lower, upper, sig) in enumerate(\n    zip(coefficients, ci_lower, ci_upper, significant, strict=False)\n):\n    y_pos = i + 1\n    if sig:\n        sig_points.append((coef, y_pos))\n        ci_sig.append(((lower, y_pos), (upper, y_pos)))\n    else:\n        nonsig_points.append((coef, y_pos))\n        ci_nonsig.append(((lower, y_pos), (upper, y_pos)))\n\n# Add point series (significant first for color order)\nif sig_points:\n    chart.add(\"Significant (p < 0.05)\", sig_points, color=BRAND, dots_size=18)\nif nonsig_points:\n    chart.add(\"Not Significant\", nonsig_points, color=MUTED, dots_size=18)\n\n# Add confidence interval lines as horizontal lines\nfor (lower, y), (upper, y) in ci_sig:\n    chart.add(\n        None,\n        [(lower, y), (upper, y)],\n        stroke=True,\n        show_dots=False,\n        stroke_style={\"width\": 4, \"linecap\": \"round\"},\n        color=BRAND,\n    )\n\nfor (lower, y), (upper, y) in ci_nonsig:\n    chart.add(\n        None,\n        [(lower, y), (upper, y)],\n        stroke=True,\n        show_dots=False,\n        stroke_style={\"width\": 4, \"linecap\": \"round\"},\n        color=MUTED,\n    )\n\n# Add vertical reference line at zero\nzero_line = [(0, 0), (0, n_vars + 1)]\nchart.add(\n    \"Zero Reference\",\n    zero_line,\n    stroke=True,\n    show_dots=False,\n    stroke_style={\"width\": 3, \"dasharray\": \"8,4\"},\n    color=INK_SOFT,\n)\n\n# Save outputs\nchart.render_to_png(f\"plot-{THEME}.png\")\nwith open(f\"plot-{THEME}.html\", \"wb\") as f:\n    f.write(chart.render())\n"}