{"spec_id":"bar-feature-importance","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nbar-feature-importance: Feature Importance Bar Chart\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-10\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Data - Feature importances from a machine learning model\nnp.random.seed(42)\nfeatures = [\n    \"Income\",\n    \"Credit Score\",\n    \"Age\",\n    \"Employment Years\",\n    \"Debt Ratio\",\n    \"Number of Accounts\",\n    \"Payment History\",\n    \"Loan Amount\",\n    \"Education Level\",\n    \"Home Ownership\",\n    \"Marital Status\",\n    \"Number of Dependents\",\n]\n\nimportance = np.array([0.182, 0.156, 0.124, 0.098, 0.089, 0.078, 0.072, 0.065, 0.051, 0.042, 0.028, 0.015])\nstd = np.array([0.025, 0.022, 0.018, 0.015, 0.014, 0.012, 0.011, 0.010, 0.008, 0.007, 0.005, 0.003])\n\n# Sort by importance (highest at top for horizontal bar chart)\nsorted_indices = np.argsort(importance)\nfeatures_sorted = [features[i] for i in sorted_indices]\nimportance_sorted = importance[sorted_indices]\nstd_sorted = std[sorted_indices]\n\n# Create color gradient mapped to importance values using viridis\ncmap = plt.cm.viridis\nnorm = plt.Normalize(vmin=importance_sorted.min(), vmax=importance_sorted.max())\ncolors = cmap(norm(importance_sorted))\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nbars = ax.barh(\n    features_sorted,\n    importance_sorted,\n    xerr=std_sorted,\n    color=colors,\n    edgecolor=INK_SOFT,\n    linewidth=1.5,\n    capsize=5,\n    error_kw={\"elinewidth\": 2, \"capthick\": 2, \"alpha\": 0.8, \"ecolor\": INK_SOFT},\n)\n\n# Add value annotations at the end of bars\nfor bar, val, err in zip(bars, importance_sorted, std_sorted, strict=True):\n    ax.text(\n        val + err + 0.008,\n        bar.get_y() + bar.get_height() / 2,\n        f\"{val:.3f}\",\n        va=\"center\",\n        ha=\"left\",\n        fontsize=14,\n        color=INK_SOFT,\n    )\n\n# Labels and styling\nax.set_xlabel(\"Importance Score (normalized)\", fontsize=20, color=INK)\nax.set_ylabel(\"Feature\", fontsize=20, color=INK)\nax.set_title(\"bar-feature-importance · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.set_xlim(0, importance_sorted.max() + std_sorted.max() + 0.05)\n\n# Grid\nax.grid(True, axis=\"x\", alpha=0.15, linewidth=0.8, color=INK_SOFT)\n\n# Spines\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}