{"spec_id":"bar-feature-importance","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nbar-feature-importance: Feature Importance Bar Chart\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-10\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme tokens (see prompts/default-style-guide.md \"Theme-adaptive Chrome\")\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1\n\n# Data - Feature importances from a typical ML model\nnp.random.seed(42)\n\nfeatures = [\n    \"age\",\n    \"income\",\n    \"credit_score\",\n    \"employment_years\",\n    \"debt_ratio\",\n    \"num_accounts\",\n    \"payment_history\",\n    \"loan_amount\",\n    \"property_value\",\n    \"monthly_expenses\",\n    \"education_level\",\n    \"marital_status\",\n    \"num_dependents\",\n    \"savings_balance\",\n    \"investment_portfolio\",\n]\n\n# Generate realistic importance values (decreasing with some variation)\nbase_importance = np.array(\n    [0.18, 0.15, 0.14, 0.11, 0.09, 0.07, 0.06, 0.05, 0.04, 0.03, 0.025, 0.02, 0.015, 0.012, 0.008]\n)\nimportance = base_importance + np.random.uniform(-0.005, 0.005, len(base_importance))\nimportance = np.clip(importance, 0.001, None)\n\n# Standard deviation for error bars (ensemble uncertainty)\nstd = importance * np.random.uniform(0.1, 0.3, len(importance))\n\n# Sort by importance (descending)\nsorted_idx = np.argsort(importance)[::-1]\nfeatures_sorted = [features[i] for i in sorted_idx]\nimportance_sorted = importance[sorted_idx]\nstd_sorted = std[sorted_idx]\n\n# Create color gradient based on importance using Okabe-Ito brand color\nnormalized_importance = importance_sorted / max(importance_sorted)\ncolors = [f\"rgba(0, 158, 115, {0.3 + 0.7 * norm})\" for norm in normalized_importance]\n\n# Create figure\nfig = go.Figure()\n\nfig.add_trace(\n    go.Bar(\n        y=features_sorted,\n        x=importance_sorted,\n        orientation=\"h\",\n        marker=dict(color=colors, line=dict(color=BRAND, width=1)),\n        error_x=dict(type=\"data\", array=std_sorted, color=INK_SOFT, thickness=2, width=6),\n        hovertemplate=\"<b>%{y}</b><br>Importance: %{x:.4f}<extra></extra>\",\n    )\n)\n\n# Add text annotations positioned after error bars\nfor feat, imp, std_val in zip(features_sorted, importance_sorted, std_sorted, strict=True):\n    fig.add_annotation(\n        x=imp + std_val + 0.008,\n        y=feat,\n        text=f\"{imp:.3f}\",\n        showarrow=False,\n        font=dict(size=16, color=INK_SOFT),\n        xanchor=\"left\",\n    )\n\n# Layout for 4800x2700 px canvas\nfig.update_layout(\n    title=dict(\n        text=\"bar-feature-importance · plotly · anyplot.ai\", font=dict(size=28, color=INK), x=0.5, xanchor=\"center\"\n    ),\n    xaxis=dict(\n        title=dict(text=\"Importance Score\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        gridwidth=1,\n        range=[0, max(importance_sorted) + max(std_sorted) + 0.035],\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n    ),\n    yaxis=dict(tickfont=dict(size=18, color=INK_SOFT), autorange=\"reversed\", linecolor=INK_SOFT),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font=dict(color=INK),\n    margin=dict(l=180, r=100, t=100, b=80),\n    showlegend=False,\n)\n\n# Save as PNG and HTML\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}