{"spec_id":"waffle-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nwaffle-basic: Basic Waffle Chart\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 79/100 | Created: 2026-05-05\n\"\"\"\n\nimport os\nimport sys\n\nimport pandas as pd\n\n\nsys.path.pop(0)\nfrom plotnine import (\n    aes,\n    coord_equal,\n    element_blank,\n    element_rect,\n    element_text,\n    geom_tile,\n    ggplot,\n    guide_legend,\n    guides,\n    labs,\n    scale_fill_manual,\n    theme,\n    theme_minimal,\n)\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\"\n\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\"]\n\n# Data - Budget allocation by category\ncategories = [\"Housing\", \"Food\", \"Transport\", \"Entertainment\"]\nvalues = [35, 25, 18, 12]\ncategory_order = categories\n\n# Create waffle grid (10x10 = 100 squares)\ngrid_size = 10\nsquares = []\nsquare_id = 0\n\nfor val, cat in zip(values, categories, strict=True):\n    for _ in range(val):\n        row = square_id // grid_size\n        col = square_id % grid_size\n        squares.append({\"x\": col, \"y\": grid_size - 1 - row, \"category\": cat})\n        square_id += 1\n\ndf = pd.DataFrame(squares)\ndf[\"category\"] = pd.Categorical(df[\"category\"], categories=category_order, ordered=True)\n\n# Create color mapping with Okabe-Ito palette\ncolor_map = dict(zip(category_order, IMPRINT, strict=True))\nvalue_map = dict(zip(categories, values, strict=True))\n\n# Build legend labels with percentages\nlegend_labels = {cat: f\"{cat} ({value_map[cat]}%)\" for cat in category_order}\n\n# Theme configuration\nanyplot_theme = theme(\n    figure_size=(16, 9),\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    plot_title=element_text(size=24, color=INK, ha=\"center\"),\n    legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n    legend_title=element_text(size=18, color=INK),\n    legend_text=element_text(size=16, color=INK_SOFT),\n    legend_position=\"right\",\n    axis_text=element_blank(),\n    axis_title=element_blank(),\n    axis_ticks=element_blank(),\n    panel_grid=element_blank(),\n)\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"x\", y=\"y\", fill=\"category\"))\n    + geom_tile(color=PAGE_BG, size=0.8)\n    + scale_fill_manual(values=color_map, labels=lambda x: [legend_labels[c] for c in x])\n    + coord_equal()\n    + labs(title=\"waffle-basic · plotnine · anyplot.ai\", fill=\"Category\")\n    + guides(fill=guide_legend(ncol=1))\n    + theme_minimal()\n    + anyplot_theme\n)\n\nplot.save(f\"plot-{THEME}.png\", dpi=300, verbose=False)\n"}