{"spec_id":"bar-permutation-importance","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nbar-permutation-importance: Permutation Feature Importance Plot\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 84/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom sklearn.datasets import load_breast_cancer\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.inspection import permutation_importance\n\n\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\"\n\n# Theme-adaptive setup\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n    },\n)\n\n# Data - Using breast cancer dataset with permutation importance\ndata = load_breast_cancer()\nX, y = data.data, data.target\nfeature_names = data.feature_names\n\n# Train a Random Forest model\nnp.random.seed(42)\nclf = RandomForestClassifier(n_estimators=100, random_state=42)\nclf.fit(X, y)\n\n# Calculate permutation importance\nperm_importance = permutation_importance(clf, X, y, n_repeats=10, random_state=42)\n\n# Extract importance values\nimportance_mean = perm_importance.importances_mean\nimportance_std = perm_importance.importances_std\n\n# Sort by importance (descending)\nsorted_idx = np.argsort(importance_mean)[::-1]\nsorted_features = [feature_names[i] for i in sorted_idx]\nsorted_mean = importance_mean[sorted_idx]\nsorted_std = importance_std[sorted_idx]\n\n# Create plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Create horizontal bar plot with viridis palette\ncolors = sns.color_palette(\"viridis\", n_colors=len(sorted_features))\ncolor_order = list(reversed(colors))\n\ny_positions = np.arange(len(sorted_features))\nsns.barplot(x=sorted_mean, y=sorted_features, hue=sorted_features, palette=color_order, ax=ax, orient=\"h\", legend=False)\n\n# Add error bars manually\nax.errorbar(sorted_mean, y_positions, xerr=sorted_std, fmt=\"none\", ecolor=INK_SOFT, elinewidth=2, capsize=5, capthick=2)\n\n# Add vertical reference line at x=0\nax.axvline(x=0, color=INK_SOFT, linestyle=\"-\", linewidth=1.5, alpha=0.7)\n\n# Style\nax.set_xlabel(\"Mean Importance (Decrease in Accuracy)\", fontsize=20, color=INK)\nax.set_ylabel(\"Feature\", fontsize=20, color=INK)\nax.set_title(\"bar-permutation-importance · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Grid\nax.yaxis.grid(False)\nax.xaxis.grid(True, alpha=0.10, linewidth=0.8, linestyle=\"-\")\n\n# Remove top and right 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"}