{"spec_id":"facet-grid","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nfacet-grid: Faceted Grid Plot\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nimport plotly.express as px\n\n\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nBRAND = \"#009E73\"\n\n# Data\nnp.random.seed(42)\n\nregions = [\"North\", \"South\", \"East\", \"West\"]\nquarters = [\"Q1\", \"Q2\", \"Q3\", \"Q4\"]\n\ndata = []\nfor region in regions:\n    for quarter in quarters:\n        n = 15\n        base_sales = {\"North\": 80, \"South\": 60, \"East\": 70, \"West\": 75}\n        quarter_effect = {\"Q1\": 0, \"Q2\": 10, \"Q3\": 5, \"Q4\": 15}\n\n        marketing_spend = np.random.uniform(10, 50, n)\n        sales = (\n            base_sales[region]\n            + quarter_effect[quarter]\n            + marketing_spend * (0.8 + np.random.uniform(0, 0.4, n))\n            + np.random.normal(0, 5, n)\n        )\n\n        for i in range(n):\n            data.append(\n                {\n                    \"Marketing Spend ($K)\": marketing_spend[i],\n                    \"Sales ($K)\": sales[i],\n                    \"Region\": region,\n                    \"Quarter\": quarter,\n                }\n            )\n\ndf = pd.DataFrame(data)\n\n# Plot\nfig = px.scatter(df, x=\"Marketing Spend ($K)\", y=\"Sales ($K)\", facet_row=\"Region\", facet_col=\"Quarter\")\n\n# Style\nfig.update_layout(\n    title={\"text\": \"facet-grid · plotly · anyplot.ai\", \"font\": {\"size\": 28, \"color\": INK}, \"x\": 0.5, \"xanchor\": \"center\"},\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    showlegend=False,\n    margin={\"l\": 80, \"r\": 80, \"t\": 100, \"b\": 80},\n)\n\nfig.update_xaxes(\n    title_font={\"size\": 22, \"color\": INK}, tickfont={\"size\": 18, \"color\": INK_SOFT}, gridcolor=GRID, linecolor=INK_SOFT\n)\n\nfig.update_yaxes(\n    title_font={\"size\": 22, \"color\": INK}, tickfont={\"size\": 18, \"color\": INK_SOFT}, gridcolor=GRID, linecolor=INK_SOFT\n)\n\nfig.for_each_annotation(lambda a: a.update(font={\"size\": 18, \"color\": INK}))\n\nfig.update_traces(marker={\"size\": 16, \"opacity\": 0.8, \"color\": BRAND, \"line\": {\"width\": 1, \"color\": PAGE_BG}})\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}