Skip to content

Commit 1dd3c9f

Browse files
feat(pygal): implement bar-feature-importance (#2321)
## Implementation: `bar-feature-importance` - pygal Implements the **pygal** version of `bar-feature-importance`. **File:** `plots/bar-feature-importance/implementations/pygal.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20526639091)* --------- 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 25a23de commit 1dd3c9f

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
""" pyplots.ai
2+
bar-feature-importance: Feature Importance Bar Chart
3+
Library: pygal 3.1.0 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-26
5+
"""
6+
7+
import pygal
8+
from pygal.style import Style
9+
10+
11+
# Data - Feature importances from a RandomForest classifier (house price prediction)
12+
features = [
13+
"OverallQual",
14+
"GrLivArea",
15+
"TotalBsmtSF",
16+
"GarageCars",
17+
"YearBuilt",
18+
"FullBath",
19+
"TotRmsAbvGrd",
20+
"Fireplaces",
21+
"BsmtQual",
22+
"LotArea",
23+
"GarageArea",
24+
"YearRemodAdd",
25+
"KitchenQual",
26+
"Neighborhood",
27+
"ExterQual",
28+
]
29+
importances = [0.245, 0.182, 0.098, 0.087, 0.076, 0.054, 0.048, 0.042, 0.038, 0.035, 0.031, 0.024, 0.019, 0.012, 0.009]
30+
31+
# Sort by importance (descending) - pygal renders bottom to top, so reverse for highest at top
32+
sorted_pairs = sorted(zip(features, importances, strict=True), key=lambda x: x[1], reverse=False)
33+
sorted_features = [p[0] for p in sorted_pairs]
34+
sorted_importances = [p[1] for p in sorted_pairs]
35+
36+
# Generate colors from light blue to Python Blue based on importance
37+
min_imp = min(sorted_importances)
38+
max_imp = max(sorted_importances)
39+
40+
41+
def importance_to_color(value):
42+
"""Map importance to color gradient: light blue -> Python Blue (#306998)"""
43+
if max_imp == min_imp:
44+
ratio = 1
45+
else:
46+
ratio = (value - min_imp) / (max_imp - min_imp)
47+
# Interpolate from light blue (180, 210, 240) to Python Blue (48, 105, 152)
48+
r = int(180 - ratio * (180 - 48))
49+
g = int(210 - ratio * (210 - 105))
50+
b = int(240 - ratio * (240 - 152))
51+
return f"#{r:02x}{g:02x}{b:02x}"
52+
53+
54+
# Custom style for large canvas (4800 x 2700 px)
55+
custom_style = Style(
56+
background="white",
57+
plot_background="white",
58+
foreground="#333333",
59+
foreground_strong="#333333",
60+
foreground_subtle="#666666",
61+
colors=("#306998",),
62+
font_family="sans-serif",
63+
title_font_size=72,
64+
label_font_size=42,
65+
major_label_font_size=38,
66+
legend_font_size=40,
67+
value_font_size=36,
68+
value_colors=("#333333",),
69+
stroke_width=0,
70+
)
71+
72+
# Create horizontal bar chart
73+
chart = pygal.HorizontalBar(
74+
width=4800,
75+
height=2700,
76+
style=custom_style,
77+
title="bar-feature-importance · pygal · pyplots.ai",
78+
x_title="Importance Score",
79+
show_legend=False,
80+
show_y_guides=False,
81+
show_x_guides=True,
82+
print_values=True,
83+
print_values_position="top",
84+
value_formatter=lambda x: f"{x:.3f}",
85+
margin=60,
86+
spacing=15,
87+
truncate_label=-1,
88+
x_label_rotation=0,
89+
)
90+
91+
# Set feature names as y-axis labels (x_labels become y-axis in horizontal chart)
92+
chart.x_labels = sorted_features
93+
94+
# Add data with individual colors per bar
95+
bar_data = [{"value": imp, "color": importance_to_color(imp)} for imp in sorted_importances]
96+
chart.add("Importance", bar_data)
97+
98+
# Save outputs
99+
chart.render_to_file("plot.html")
100+
chart.render_to_png("plot.png")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library: pygal
2+
specification_id: bar-feature-importance
3+
created: '2025-12-26T17:41:36Z'
4+
updated: '2025-12-26T17:52:21Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20526639091
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 3.1.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/bar-feature-importance/pygal/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/bar-feature-importance/pygal/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/bar-feature-importance/pygal/plot.html
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent use of pygal per-bar color styling with gradient mapped to importance
17+
values
18+
- Clean visual design with appropriate font sizes for 4800x2700 canvas
19+
- Correct sorting with highest importance at top (pygal renders bottom-to-top, code
20+
handles this)
21+
- Value annotations positioned clearly at end of each bar with 3 decimal precision
22+
- Realistic house price prediction context using recognizable feature names
23+
weaknesses:
24+
- Contains a helper function importance_to_color which deviates from strict KISS
25+
script style
26+
- X-axis could be tighter to data range to reduce unused whitespace on right side

0 commit comments

Comments
 (0)