Skip to content

Commit d0bfd78

Browse files
feat(seaborn): implement bar-permutation-importance (#3024)
## Implementation: `bar-permutation-importance` - seaborn Implements the **seaborn** version of `bar-permutation-importance`. **File:** `plots/bar-permutation-importance/implementations/seaborn.py` **Parent Issue:** #2998 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20617498003)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 257ec53 commit d0bfd78

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
""" pyplots.ai
2+
bar-permutation-importance: Permutation Feature Importance Plot
3+
Library: seaborn 0.13.2 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-31
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
import seaborn as sns
10+
from sklearn.datasets import load_wine
11+
from sklearn.ensemble import RandomForestClassifier
12+
from sklearn.inspection import permutation_importance
13+
14+
15+
# Data - Using wine dataset with permutation importance from Random Forest
16+
wine = load_wine()
17+
X, y = wine.data, wine.target
18+
feature_names = wine.feature_names
19+
20+
# Train a Random Forest model
21+
np.random.seed(42)
22+
clf = RandomForestClassifier(n_estimators=100, random_state=42)
23+
clf.fit(X, y)
24+
25+
# Calculate permutation importance
26+
perm_importance = permutation_importance(clf, X, y, n_repeats=10, random_state=42)
27+
28+
# Extract importance values
29+
importance_mean = perm_importance.importances_mean
30+
importance_std = perm_importance.importances_std
31+
32+
# Sort by importance (descending)
33+
sorted_idx = np.argsort(importance_mean)[::-1]
34+
sorted_features = [feature_names[i] for i in sorted_idx]
35+
sorted_mean = importance_mean[sorted_idx]
36+
sorted_std = importance_std[sorted_idx]
37+
38+
# Create plot
39+
fig, ax = plt.subplots(figsize=(16, 9))
40+
41+
# Color palette based on importance values (sequential)
42+
colors = sns.color_palette("viridis", n_colors=len(sorted_features))
43+
# Map colors to sorted importance (highest importance = darkest)
44+
color_order = list(reversed(colors))
45+
46+
# Create horizontal bar plot with seaborn
47+
y_positions = np.arange(len(sorted_features))
48+
sns.barplot(x=sorted_mean, y=sorted_features, hue=sorted_features, palette=color_order, legend=False, ax=ax, orient="h")
49+
50+
# Add error bars manually (seaborn barplot doesn't support horizontal error bars directly)
51+
ax.errorbar(
52+
sorted_mean, y_positions, xerr=sorted_std, fmt="none", ecolor="#333333", elinewidth=2, capsize=5, capthick=2
53+
)
54+
55+
# Add vertical reference line at x=0
56+
ax.axvline(x=0, color="#333333", linestyle="-", linewidth=1.5, alpha=0.7)
57+
58+
# Styling
59+
ax.set_xlabel("Mean Importance (Decrease in Accuracy)", fontsize=20)
60+
ax.set_ylabel("Feature", fontsize=20)
61+
ax.set_title("bar-permutation-importance · seaborn · pyplots.ai", fontsize=24)
62+
ax.tick_params(axis="both", labelsize=16)
63+
ax.grid(True, axis="x", alpha=0.3, linestyle="--")
64+
65+
# Adjust layout
66+
plt.tight_layout()
67+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library: seaborn
2+
specification_id: bar-permutation-importance
3+
created: '2025-12-31T10:56:07Z'
4+
updated: '2025-12-31T11:06:35Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20617498003
7+
issue: 2998
8+
python_version: 3.13.11
9+
library_version: 0.13.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/bar-permutation-importance/seaborn/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/bar-permutation-importance/seaborn/plot_thumb.png
12+
preview_html: null
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- 'Excellent implementation of the spec with all required features: sorted bars,
17+
error bars, color gradient, and x=0 reference line'
18+
- Uses real scikit-learn wine dataset with actual permutation importance calculation
19+
- Proper seaborn 0.14+ API usage with hue parameter to apply palette
20+
- Text sizing follows guidelines perfectly (24/20/16pt)
21+
- Viridis palette provides excellent colorblind accessibility
22+
weaknesses:
23+
- Error bars added via matplotlib errorbar rather than exploring seaborn native
24+
error bar capabilities in barplot
25+
- Grid uses dashed style which is slightly more visually distracting than solid
26+
with low alpha

0 commit comments

Comments
 (0)