{"spec_id":"pdp-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\npdp-basic: Partial Dependence Plot\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-15\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\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\"\n\n# Set seed for reproducibility\nnp.random.seed(42)\n\n# Generate synthetic regression data (housing price prediction scenario)\nX, y = make_regression(n_samples=500, n_features=5, noise=10, random_state=42)\n\n# Feature names for context\nfeature_names = [\"Square Feet\", \"Bedrooms\", \"Age (years)\", \"Distance to City\", \"Lot Size\"]\n\n# Train a gradient boosting model\nmodel = GradientBoostingRegressor(n_estimators=100, max_depth=4, random_state=42)\nmodel.fit(X, y)\n\n# Compute partial dependence for feature 0 (Square Feet)\nfeature_idx = 0\npd_result = partial_dependence(model, X, features=[feature_idx], kind=\"average\", grid_resolution=80)\n\n# Extract values\nfeature_values = pd_result[\"grid_values\"][0]\npd_values = pd_result[\"average\"][0]\n\n# Center partial dependence at zero for easier interpretation\npd_values_centered = pd_values - pd_values.mean()\n\n# Compute confidence interval using individual predictions\npd_individual = partial_dependence(model, X, features=[feature_idx], kind=\"individual\", grid_resolution=80)\nice_lines = pd_individual[\"individual\"][0]\nice_centered = ice_lines - ice_lines.mean(axis=1, keepdims=True)\nstd_dev = np.std(ice_centered, axis=0)\nci_lower = pd_values_centered - 1.96 * std_dev / np.sqrt(len(X))\nci_upper = pd_values_centered + 1.96 * std_dev / np.sqrt(len(X))\n\n# Create figure with seaborn style\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n    },\n)\n\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\n\n# Plot confidence band\nax.fill_between(feature_values, ci_lower, ci_upper, alpha=0.25, color=BRAND, label=\"95% Confidence Interval\")\n\n# Plot main PDP line using seaborn\nsns.lineplot(\n    x=feature_values, y=pd_values_centered, ax=ax, color=BRAND, linewidth=3, label=\"Partial Dependence\", legend=False\n)\n\n# Add rug plot to show data distribution\nfeature_data = X[:, feature_idx]\nsns.rugplot(x=feature_data, ax=ax, color=INK_SOFT, height=0.03, alpha=0.5)\n\n# Add horizontal line at zero for reference\nax.axhline(y=0, color=INK_SOFT, linestyle=\"--\", linewidth=1.5, alpha=0.4)\n\n# Styling\nax.set_xlabel(f\"{feature_names[feature_idx]}\", fontsize=20, color=INK)\nax.set_ylabel(\"Partial Dependence (centered)\", fontsize=20, color=INK)\nax.set_title(\"pdp-basic · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Subtle grid\nax.yaxis.grid(True, alpha=0.2, 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\n# Legend\nhandles, labels = ax.get_legend_handles_labels()\nax.legend(\n    handles,\n    labels,\n    fontsize=16,\n    loc=\"upper left\",\n    frameon=True,\n    fancybox=False,\n    edgecolor=INK_SOFT,\n    facecolor=ELEVATED_BG,\n)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}