{"spec_id":"ice-basic","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\nice-basic: Individual Conditional Expectation (ICE) Plot\nLibrary: pygal 3.1.3 | Python 3.13.15\nQuality: 86/100 | Updated: 2026-08-17\n\"\"\"\n\nimport importlib\nimport os\nimport sys\n\nimport numpy as np\nfrom sklearn.ensemble import GradientBoostingRegressor\n\n\n# Prevent self-import: this file is named pygal.py, which shadows the package.\n# Remove the script directory from sys.path before loading the pygal package.\n_this_dir = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p or \".\") != _this_dir]\n\npygal = importlib.import_module(\"pygal\")\nStyle = importlib.import_module(\"pygal.style\").Style\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\nIMPRINT_PALETTE = (\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\")\nICE_COLOR = IMPRINT_PALETTE[0]  # Imprint palette position 1 — ICE lines\nPDP_COLOR = IMPRINT_PALETTE[1]  # Imprint palette position 2 — PDP line\nPDP_HALO_COLOR = PAGE_BG  # background-colored outline so PDP reads through dense ICE bands\n\n# Data: house price predictions from GradientBoostingRegressor\nnp.random.seed(42)\nn_obs = 50\nn_grid = 75  # spec recommends 50-100 grid points\n\nsqft = np.random.normal(1800, 450, n_obs).clip(700, 3600)\nbedrooms = np.random.randint(2, 6, n_obs)\nlocation_score = np.random.uniform(0.2, 1.0, n_obs)\nprice = 100_000 + 130 * sqft + 9_000 * bedrooms + 180_000 * location_score + np.random.normal(0, 18_000, n_obs)\n\nsqft_grid = np.linspace(700, 3600, n_grid)\n\nX = np.column_stack([sqft, bedrooms, location_score])\nmodel = GradientBoostingRegressor(n_estimators=200, max_depth=4, random_state=42)\nmodel.fit(X, price)\n\nice_curves = np.zeros((n_obs, n_grid))\nfor i in range(n_obs):\n    X_ice = np.tile([sqft[i], bedrooms[i], location_score[i]], (n_grid, 1))\n    X_ice[:, 0] = sqft_grid\n    ice_curves[i] = model.predict(X_ice) / 1_000  # convert to $K\n\npdp_curve = ice_curves.mean(axis=0)\n\n# Style: ICE lines use brand green, PDP uses lavender for contrast; a background-colored\n# halo line is drawn just beneath the PDP line so it stays legible where curves converge\ncolors_tuple = (ICE_COLOR,) * n_obs + (PDP_HALO_COLOR, PDP_COLOR)\n\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    colors=colors_tuple,\n    title_font_size=66,  # library-prompt canonical native-pixel sizing for 3200x1800\n    label_font_size=56,\n    major_label_font_size=44,\n    legend_font_size=44,\n    value_font_size=36,\n    stroke_width=2.5,\n)\n\n# Plot — cubic interpolation gives smooth ICE curves (pygal-native feature)\n# legend stays off the bottom: pygal reserves bottom margin proportional to the\n# *total* series count (all 51 lines) regardless of which ones carry a legend\n# label, so legend_at_bottom here would still reserve ~40% of canvas height for\n# a legend that only ever shows 2 rows. The default side legend only reserves\n# space for the labels actually rendered.\nchart = pygal.Line(\n    style=custom_style,\n    width=3200,\n    height=1800,\n    title=\"ice-basic · python · pygal · anyplot.ai\",\n    x_title=\"Square Footage\",\n    y_title=\"Predicted Price ($ thousands)\",\n    show_dots=False,\n    show_y_guides=True,\n    show_x_guides=False,\n    interpolate=\"cubic\",\n    x_label_rotation=30,\n    truncate_legend=-1,  # disable truncation — only 2 legend rows are ever shown\n)\n\n# x-axis: label every 10th grid point to avoid crowding (75 total)\nx_labels = [\"\"] * n_grid\nstep = max(1, n_grid // 7)\nfor idx in range(0, n_grid, step):\n    x_labels[idx] = str(int(sqft_grid[idx]))\nchart.x_labels = x_labels\n\n# ICE lines — only the first carries a legend label; the rest use title=None\n# (not \"\") so pygal's _legend() skips their row entirely instead of rendering\n# 49 blank entries.\nchart.add(\n    f\"ICE Curves (n={n_obs})\", [round(float(v), 1) for v in ice_curves[0]], stroke_style={\"width\": 2, \"opacity\": 0.3}\n)\nfor i in range(1, n_obs):\n    chart.add(None, [round(float(v), 1) for v in ice_curves[i]], stroke_style={\"width\": 2, \"opacity\": 0.3})\n\n# PDP line — a wide background-colored halo drawn first, then the bold, fully opaque\n# PDP curve on top; the halo keeps the average visible where ICE curves converge into\n# dense bands (e.g. sqft 1875-3443) instead of blending into the green mass\npdp_values = [round(float(v), 1) for v in pdp_curve]\nchart.add(None, pdp_values, stroke_style={\"width\": 18, \"opacity\": 0.95})\nchart.add(\"PDP (average)\", pdp_values, stroke_style={\"width\": 12, \"opacity\": 1.0})\n\n# Save\nchart.render_to_png(f\"plot-{THEME}.png\")\nwith open(f\"plot-{THEME}.html\", \"wb\") as f:\n    f.write(chart.render())\n"}