{"spec_id":"pdp-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\npdp-basic: Partial Dependence Plot\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-15\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn.datasets import make_regression\nfrom sklearn.ensemble import GradientBoostingRegressor\nfrom sklearn.inspection import partial_dependence\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1\nACCENT = \"#AE3030\"  # Okabe-Ito position 5 for rug plot\n\n# Data: Train a gradient boosting model and compute partial dependence\nnp.random.seed(42)\nX, y = make_regression(n_samples=500, n_features=5, noise=15, random_state=42)\n\n# Train model\nmodel = GradientBoostingRegressor(n_estimators=100, max_depth=4, random_state=42)\nmodel.fit(X, y)\n\n# Compute partial dependence for feature 0\nfeature_idx = 0\n\n# Get partial dependence using sklearn\npd_result = partial_dependence(model, X, features=[feature_idx], kind=\"both\", grid_resolution=80)\npdp_values = pd_result[\"average\"][0]\nice_lines = pd_result[\"individual\"][0]\ngrid_values = pd_result[\"grid_values\"][0]\n\n# Calculate confidence interval (mean ± std of ICE lines)\nice_mean = pdp_values\nice_std = np.std(ice_lines, axis=0)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Plot ICE lines (faint individual lines)\nfor i in range(0, len(ice_lines), 10):\n    ax.plot(grid_values, ice_lines[i], color=BRAND, alpha=0.08, linewidth=1)\n\n# Plot confidence band\nax.fill_between(\n    grid_values,\n    ice_mean - 1.96 * ice_std,\n    ice_mean + 1.96 * ice_std,\n    alpha=0.2,\n    color=BRAND,\n    label=\"95% Confidence Interval\",\n)\n\n# Plot main PDP line\nax.plot(grid_values, pdp_values, color=BRAND, linewidth=4, label=\"Partial Dependence\")\n\n# Add rug plot showing data distribution\nrug_y = ax.get_ylim()[0]\nax.scatter(\n    X[:, feature_idx], np.full(len(X), rug_y), marker=\"|\", color=ACCENT, alpha=0.5, s=200, label=\"Data Distribution\"\n)\n\n# Style\nax.set_xlabel(\"Feature Value\", fontsize=20, color=INK)\nax.set_ylabel(\"Partial Dependence (Predicted Value)\", fontsize=20, color=INK)\nax.set_title(\"pdp-basic · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Legend with background\nleg = ax.legend(fontsize=16, loc=\"upper left\", frameon=True)\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nleg.get_frame().set_linewidth(1)\nfor text in leg.get_texts():\n    text.set_color(INK_SOFT)\n\n# Grid\nax.grid(True, alpha=0.1, linewidth=0.8, color=INK)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}