Skip to content

Commit 28a4105

Browse files
feat(matplotlib): implement pdp-basic (#2957)
## Implementation: `pdp-basic` - matplotlib Implements the **matplotlib** version of `pdp-basic`. **File:** `plots/pdp-basic/implementations/matplotlib.py` **Parent Issue:** #2922 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20612797918)* --------- 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 96e693c commit 28a4105

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
""" pyplots.ai
2+
pdp-basic: Partial Dependence Plot
3+
Library: matplotlib 3.10.8 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-31
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
from sklearn.datasets import make_regression
10+
from sklearn.ensemble import GradientBoostingRegressor
11+
from sklearn.inspection import partial_dependence
12+
13+
14+
# Data: Train a gradient boosting model and compute partial dependence
15+
np.random.seed(42)
16+
X, y = make_regression(n_samples=500, n_features=5, noise=15, random_state=42)
17+
18+
# Train model
19+
model = GradientBoostingRegressor(n_estimators=100, max_depth=4, random_state=42)
20+
model.fit(X, y)
21+
22+
# Compute partial dependence for feature 0
23+
feature_idx = 0
24+
25+
# Get partial dependence using sklearn
26+
pd_result = partial_dependence(model, X, features=[feature_idx], kind="both", grid_resolution=80)
27+
pdp_values = pd_result["average"][0]
28+
ice_lines = pd_result["individual"][0]
29+
grid_values = pd_result["grid_values"][0]
30+
31+
# Calculate confidence interval (mean ± std of ICE lines)
32+
ice_mean = pdp_values
33+
ice_std = np.std(ice_lines, axis=0)
34+
35+
# Create plot (4800x2700 px)
36+
fig, ax = plt.subplots(figsize=(16, 9))
37+
38+
# Plot ICE lines (faint individual lines)
39+
for i in range(0, len(ice_lines), 10): # Sample every 10th line for clarity
40+
ax.plot(grid_values, ice_lines[i], color="#306998", alpha=0.1, linewidth=1)
41+
42+
# Plot confidence band
43+
ax.fill_between(
44+
grid_values,
45+
ice_mean - 1.96 * ice_std,
46+
ice_mean + 1.96 * ice_std,
47+
alpha=0.25,
48+
color="#306998",
49+
label="95% Confidence Interval",
50+
)
51+
52+
# Plot main PDP line
53+
ax.plot(grid_values, pdp_values, color="#306998", linewidth=4, label="Partial Dependence")
54+
55+
# Add rug plot showing data distribution
56+
rug_y = ax.get_ylim()[0]
57+
ax.scatter(
58+
X[:, feature_idx], np.full(len(X), rug_y), marker="|", color="#FFD43B", alpha=0.4, s=100, label="Data Distribution"
59+
)
60+
61+
# Labels and styling
62+
ax.set_xlabel("Feature Value", fontsize=20)
63+
ax.set_ylabel("Partial Dependence (Predicted Value)", fontsize=20)
64+
ax.set_title("pdp-basic · matplotlib · pyplots.ai", fontsize=24)
65+
ax.tick_params(axis="both", labelsize=16)
66+
ax.legend(fontsize=16, loc="upper left")
67+
ax.grid(True, alpha=0.3, linestyle="--")
68+
69+
plt.tight_layout()
70+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library: matplotlib
2+
specification_id: pdp-basic
3+
created: '2025-12-31T05:34:35Z'
4+
updated: '2025-12-31T05:46:31Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20612797918
7+
issue: 2922
8+
python_version: 3.13.11
9+
library_version: 3.10.8
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/pdp-basic/matplotlib/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/pdp-basic/matplotlib/plot_thumb.png
12+
preview_html: null
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent implementation of PDP with ICE lines using sklearn partial_dependence
17+
function with kind=both
18+
- Proper confidence band visualization using fill_between with mean ± 1.96*std
19+
- Rug plot effectively shows training data distribution along x-axis
20+
- Clean, readable code that follows KISS principles
21+
- Good colorblind-safe color scheme with Python blue and yellow
22+
- Appropriate text sizing for 4800x2700 canvas
23+
weaknesses:
24+
- Legend in upper left slightly overlaps with ICE lines - consider frameon=True
25+
with white background
26+
- Rug plot markers (s=100) could be larger (s=150-200) for better visibility on
27+
high-resolution canvas
28+
- Y-axis range dominated by wide confidence band making PDP line variation appear
29+
minimal

0 commit comments

Comments
 (0)