{"spec_id":"facet-grid","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nfacet-grid: Faceted Grid Plot\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 96/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove the script's directory from sys.path to avoid shadowing matplotlib package\nscript_dir = os.path.dirname(os.path.abspath(__file__))\nif script_dir in sys.path:\n    sys.path.remove(script_dir)\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom matplotlib import gridspec\nfrom scipy.stats import linregress\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Okabe-Ito palette - first series is always #009E73\nIMPRINT = [\n    \"#009E73\",  # brand green\n    \"#C475FD\",  # vermillion\n    \"#4467A3\",  # blue\n    \"#BD8233\",  # reddish purple\n]\n\n# Data\nnp.random.seed(42)\n\nregions = [\"North\", \"South\", \"East\"]\nseasons = [\"Spring\", \"Summer\", \"Fall\", \"Winter\"]\n\ndata = []\nfor region in regions:\n    for season in seasons:\n        n_points = 25\n        # Base temperature varies by region and season\n        base_temps = {\"North\": 5, \"South\": 20, \"East\": 15}\n        season_offsets = {\"Spring\": 5, \"Summer\": 10, \"Fall\": 2, \"Winter\": -8}\n\n        base_temp = base_temps[region] + season_offsets[season]\n        temp = np.random.normal(base_temp, 4, n_points)\n\n        # Energy consumption: U-shaped relationship with temperature\n        energy = 120 + (temp - base_temp) ** 2 * 0.2 + np.random.normal(0, 8, n_points)\n\n        for t, e in zip(temp, energy, strict=True):\n            data.append({\"Temperature\": t, \"Energy\": e, \"Region\": region, \"Season\": season})\n\ndf = pd.DataFrame(data)\n\n# Create figure with GridSpec for sophisticated layout control\nfig = plt.figure(figsize=(16, 9), facecolor=PAGE_BG)\ngs = gridspec.GridSpec(\n    len(regions), len(seasons), figure=fig, hspace=0.35, wspace=0.3, left=0.08, right=0.98, top=0.92, bottom=0.10\n)\n\n# Color map: regions to Okabe-Ito palette\nregion_colors = {region: IMPRINT[i] for i, region in enumerate(regions)}\n\n# Create scatter plots in each facet with trend lines\nfor i, region in enumerate(regions):\n    for j, season in enumerate(seasons):\n        ax = fig.add_subplot(gs[i, j])\n        ax.set_facecolor(PAGE_BG)\n\n        subset = df[(df[\"Region\"] == region) & (df[\"Season\"] == season)]\n\n        # Scatter plot\n        ax.scatter(\n            subset[\"Temperature\"],\n            subset[\"Energy\"],\n            s=120,\n            alpha=0.7,\n            color=region_colors[region],\n            edgecolors=\"white\",\n            linewidth=0.8,\n            zorder=3,\n        )\n\n        # Trend line for visual emphasis and pattern highlighting\n        if len(subset) > 1:\n            x_sorted = np.sort(subset[\"Temperature\"].values)\n            slope, intercept, _, _, _ = linregress(subset[\"Temperature\"].values, subset[\"Energy\"].values)\n            y_trend = slope * x_sorted + intercept\n            ax.plot(x_sorted, y_trend, color=INK_MUTED, linewidth=2.5, alpha=0.4, linestyle=\"-\", zorder=1)\n\n        # Subtle grid\n        ax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK)\n        ax.xaxis.grid(True, alpha=0.08, linewidth=0.6, color=INK_SOFT)\n\n        # Column headers (top row only)\n        if i == 0:\n            ax.set_title(season, fontsize=20, color=INK, fontweight=\"semibold\")\n\n        # Row labels (left side)\n        if j == 0:\n            ax.set_ylabel(region, fontsize=20, color=INK, fontweight=\"semibold\")\n\n        # Only show labels on outer edges\n        if i < len(regions) - 1:\n            ax.set_xticklabels([])\n        if j > 0:\n            ax.set_yticklabels([])\n\n        # Tick styling\n        ax.tick_params(axis=\"both\", labelsize=14, colors=INK_SOFT, labelcolor=INK_SOFT, length=5, width=0.8)\n\n        # Spine styling\n        for spine in (\"top\", \"right\"):\n            ax.spines[spine].set_visible(False)\n        for spine in (\"left\", \"bottom\"):\n            ax.spines[spine].set_color(INK_SOFT)\n            ax.spines[spine].set_linewidth(1.2)\n\n# Shared axis labels\nfig.text(0.5, 0.02, \"Temperature (°C)\", ha=\"center\", fontsize=22, color=INK, fontweight=\"medium\")\nfig.text(\n    0.01, 0.5, \"Energy Consumption (kWh)\", va=\"center\", rotation=\"vertical\", fontsize=22, color=INK, fontweight=\"medium\"\n)\n\n# Main title with enhanced typography\nfig.suptitle(\"facet-grid · matplotlib · anyplot.ai\", fontsize=26, fontweight=\"semibold\", color=INK, y=0.97)\n\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}