Skip to content

Commit 13a24bd

Browse files
feat(matplotlib): implement bar-feature-importance (#2289)
## Implementation: `bar-feature-importance` - matplotlib Implements the **matplotlib** version of `bar-feature-importance`. **File:** `plots/bar-feature-importance/implementations/matplotlib.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20526602154)* --------- 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 d67f408 commit 13a24bd

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
""" pyplots.ai
2+
bar-feature-importance: Feature Importance Bar Chart
3+
Library: matplotlib 3.10.8 | Python 3.13.11
4+
Quality: 94/100 | Created: 2025-12-26
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
10+
11+
# Data - Feature importances from a RandomForest model
12+
np.random.seed(42)
13+
features = [
14+
"Income",
15+
"Credit Score",
16+
"Age",
17+
"Employment Years",
18+
"Debt Ratio",
19+
"Number of Accounts",
20+
"Payment History",
21+
"Loan Amount",
22+
"Education Level",
23+
"Home Ownership",
24+
"Marital Status",
25+
"Number of Dependents",
26+
]
27+
28+
# Generate realistic importance values (sorted descending)
29+
importance = 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])
30+
std = 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])
31+
32+
# Sort by importance (highest at top for horizontal bar chart)
33+
sorted_indices = np.argsort(importance)
34+
features_sorted = [features[i] for i in sorted_indices]
35+
importance_sorted = importance[sorted_indices]
36+
std_sorted = std[sorted_indices]
37+
38+
# Create color gradient based on importance values
39+
cmap = plt.cm.Blues
40+
colors = cmap(0.3 + 0.6 * (importance_sorted / importance_sorted.max()))
41+
42+
# Plot
43+
fig, ax = plt.subplots(figsize=(16, 9))
44+
45+
bars = ax.barh(
46+
features_sorted,
47+
importance_sorted,
48+
xerr=std_sorted,
49+
color=colors,
50+
edgecolor="#306998",
51+
linewidth=1.5,
52+
capsize=5,
53+
error_kw={"elinewidth": 2, "capthick": 2, "alpha": 0.7},
54+
)
55+
56+
# Add value annotations at the end of bars
57+
for bar, val, err in zip(bars, importance_sorted, std_sorted, strict=True):
58+
ax.text(
59+
val + err + 0.008,
60+
bar.get_y() + bar.get_height() / 2,
61+
f"{val:.3f}",
62+
va="center",
63+
ha="left",
64+
fontsize=14,
65+
color="#333333",
66+
)
67+
68+
# Labels and styling
69+
ax.set_xlabel("Importance Score", fontsize=20)
70+
ax.set_ylabel("Feature", fontsize=20)
71+
ax.set_title("bar-feature-importance · matplotlib · pyplots.ai", fontsize=24)
72+
ax.tick_params(axis="both", labelsize=16)
73+
ax.set_xlim(0, importance_sorted.max() + std_sorted.max() + 0.05)
74+
ax.grid(True, axis="x", alpha=0.3, linestyle="--")
75+
76+
# Add subtle spine styling
77+
ax.spines["top"].set_visible(False)
78+
ax.spines["right"].set_visible(False)
79+
80+
plt.tight_layout()
81+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
library: matplotlib
2+
specification_id: bar-feature-importance
3+
created: '2025-12-26T17:37:25Z'
4+
updated: '2025-12-26T17:45:28Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20526602154
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 3.10.8
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/bar-feature-importance/matplotlib/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/bar-feature-importance/matplotlib/plot_thumb.png
12+
preview_html: null
13+
quality_score: 94
14+
review:
15+
strengths:
16+
- 'Excellent implementation of all spec requirements: sorted bars, color gradient,
17+
error bars, and value annotations'
18+
- Clean code structure following KISS principles with proper seed for reproducibility
19+
- Professional styling with hidden top/right spines and subtle grid
20+
- Realistic credit scoring context with appropriate feature names and importance
21+
values
22+
- Good use of Blues colormap for sequential color gradient mapped to importance
23+
values
24+
weaknesses:
25+
- Axis labels could include units (e.g., "Importance Score (normalized)" or similar)
26+
- Could leverage matplotlib annotate() for more styled value labels with arrows
27+
or backgrounds

0 commit comments

Comments
 (0)