{"spec_id":"bar-permutation-importance","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nbar-permutation-importance: Permutation Feature Importance Plot\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-17\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\"\n\n# Data: Simulated permutation importance (resembles sklearn.inspection output)\nnp.random.seed(42)\n\nfeature_names = [\n    \"alcohol\",\n    \"malic_acid\",\n    \"ash\",\n    \"alcalinity_of_ash\",\n    \"magnesium\",\n    \"total_phenols\",\n    \"flavanoids\",\n    \"nonflavanoid_phenols\",\n    \"proanthocyanins\",\n    \"color_intensity\",\n    \"hue\",\n    \"od280/od315_of_diluted_wines\",\n    \"proline\",\n]\n\nimportance_mean = np.array([0.032, 0.003, -0.002, 0.008, 0.012, 0.048, 0.142, 0.001, 0.018, 0.095, 0.055, 0.068, 0.105])\nimportance_std = np.array([0.015, 0.008, 0.006, 0.010, 0.009, 0.020, 0.025, 0.005, 0.012, 0.022, 0.018, 0.019, 0.023])\n\n# Sort by importance (highest at top)\nsorted_idx = np.argsort(importance_mean)\nfeature_names_sorted = [feature_names[i] for i in sorted_idx]\nimportance_mean_sorted = importance_mean[sorted_idx]\nimportance_std_sorted = importance_std[sorted_idx]\n\n# Color gradient based on importance values\nnorm = plt.Normalize(importance_mean_sorted.min(), importance_mean_sorted.max())\ncmap = plt.cm.Blues\ncolors = cmap(norm(importance_mean_sorted))\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\ny_pos = np.arange(len(feature_names_sorted))\nax.barh(\n    y_pos,\n    importance_mean_sorted,\n    xerr=importance_std_sorted,\n    color=colors,\n    edgecolor=INK_SOFT,\n    linewidth=1.5,\n    height=0.7,\n    capsize=5,\n    error_kw={\"elinewidth\": 2, \"capthick\": 2, \"ecolor\": INK_SOFT},\n)\n\n# Reference line at x=0\nax.axvline(x=0, color=INK_SOFT, linewidth=2, linestyle=\"-\", alpha=0.8)\n\n# Styling\nax.set_yticks(y_pos)\nax.set_yticklabels(feature_names_sorted, fontsize=16, color=INK_SOFT)\nax.set_xlabel(\"Mean Decrease in Accuracy\", fontsize=20, color=INK)\nax.set_ylabel(\"Feature\", fontsize=20, color=INK)\nax.set_title(\"bar-permutation-importance · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"x\", labelsize=16, colors=INK_SOFT)\nax.tick_params(axis=\"y\", colors=INK_SOFT)\n\n# Grid\nax.grid(True, axis=\"x\", alpha=0.15, linestyle=\"-\", linewidth=0.8, color=INK)\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\n# Colorbar\nsm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)\nsm.set_array([])\ncbar = plt.colorbar(sm, ax=ax, pad=0.02)\ncbar.set_label(\"Importance\", fontsize=16, color=INK)\ncbar.ax.tick_params(labelsize=14, colors=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}