{"spec_id":"facet-grid","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nfacet-grid: Faceted Grid Plot\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\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\"\nBRAND = \"#009E73\"\n\n# Data - Differentiated scenario: Production Cost vs Profit Margin by Product Line and Month\nnp.random.seed(42)\n\nproduct_lines = [\"Electronics\", \"Apparel\", \"Food\"]\nmonths = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"]\n\ndata = []\nfor product_idx, product in enumerate(product_lines):\n    for month_idx, month in enumerate(months):\n        n_points = 30\n        # Vary profit margin by product line (Electronics: high margin but higher cost,\n        # Apparel: moderate, Food: low margin, high volume)\n        base_margin = 15 + 10 * product_idx\n        margin_noise = np.random.normal(0, 3, n_points)\n\n        # Cost varies by month (seasonality)\n        base_cost = 800 + 200 * month_idx\n        cost_var = np.random.uniform(-100, 100, n_points)\n\n        # Profit margin increases with cost for some products\n        cost = base_cost + cost_var\n        profit_margin = base_margin + 0.01 * (cost - base_cost) + margin_noise\n        profit_margin = np.clip(profit_margin, 5, 40)\n\n        for i in range(n_points):\n            data.append(\n                {\n                    \"Production Cost ($)\": cost[i],\n                    \"Profit Margin (%)\": profit_margin[i],\n                    \"Product Line\": product,\n                    \"Month\": month,\n                }\n            )\n\ndf = pd.DataFrame(data)\n\n# Setup theme\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_MUTED,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Plot\ng = sns.FacetGrid(df, row=\"Product Line\", col=\"Month\", height=3.8, aspect=1.1, margin_titles=True)\n\n# Map scatterplot with regression line\ng.map_dataframe(\n    sns.regplot,\n    x=\"Production Cost ($)\",\n    y=\"Profit Margin (%)\",\n    scatter_kws={\"color\": BRAND, \"s\": 140, \"alpha\": 0.75, \"edgecolor\": PAGE_BG, \"linewidths\": 0.8},\n    line_kws={\"color\": INK_SOFT, \"linewidth\": 2.5, \"alpha\": 0.6},\n    ci=None,\n)\n\n# Styling\ng.set_titles(row_template=\"{row_name}\", col_template=\"{col_name}\", size=18, fontweight=\"medium\")\n\n# Remove y-axis label repetition: only show on leftmost column\nfor i, ax in enumerate(g.axes.flat):\n    ax.tick_params(axis=\"both\", labelsize=14)\n    ax.grid(True, alpha=0.12, linestyle=\"-\", linewidth=0.7)\n\n    # Only leftmost column keeps y-axis label\n    if i % 4 != 0:\n        ax.set_ylabel(\"\")\n\n# Set labels once globally\ng.set_axis_labels(\"Production Cost ($)\", \"Profit Margin (%)\", fontsize=18)\n\n# Add main title\ng.figure.suptitle(\"facet-grid · seaborn · anyplot.ai\", fontsize=26, fontweight=\"medium\", y=0.995, color=INK)\n\ng.tight_layout()\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}