{"spec_id":"sudoku-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nsudoku-basic: Basic Sudoku Grid\nLibrary: plotnine 0.15.7 | Python 3.13.14\nQuality: 87/100 | Updated: 2026-06-25\n\"\"\"\n\nimport sys\n\n\nsys.path.pop(0)  # prevent this file from shadowing the plotnine package\n\nimport os\n\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    coord_fixed,\n    element_rect,\n    element_text,\n    geom_segment,\n    geom_text,\n    geom_tile,\n    ggplot,\n    labs,\n    theme,\n    theme_void,\n)\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_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Data - a classic partially filled Sudoku puzzle\ngrid = [\n    [5, 3, 0, 0, 7, 0, 0, 0, 0],\n    [6, 0, 0, 1, 9, 5, 0, 0, 0],\n    [0, 9, 8, 0, 0, 0, 0, 6, 0],\n    [8, 0, 0, 0, 6, 0, 0, 0, 3],\n    [4, 0, 0, 8, 0, 3, 0, 0, 1],\n    [7, 0, 0, 0, 2, 0, 0, 0, 6],\n    [0, 6, 0, 0, 0, 0, 2, 8, 0],\n    [0, 0, 0, 4, 1, 9, 0, 0, 5],\n    [0, 0, 0, 0, 8, 0, 0, 7, 9],\n]\n\n# Cell number positions (row 0 at top → y=8.5)\ncells = []\nfor row in range(9):\n    for col in range(9):\n        value = grid[row][col]\n        cells.append({\"x\": col + 0.5, \"y\": 8.5 - row, \"value\": str(value) if value != 0 else \"\"})\ndf_cells = pd.DataFrame(cells)\n\n# Alternating 3×3 box backgrounds — subtle checkerboard for visual box differentiation\nboxes_a = []  # PAGE_BG shade\nboxes_b = []  # ELEVATED_BG shade\nfor br in range(3):\n    for bc in range(3):\n        cx = bc * 3 + 1.5\n        cy = (2 - br) * 3 + 1.5\n        if (br + bc) % 2 == 0:\n            boxes_a.append({\"x\": cx, \"y\": cy})\n        else:\n            boxes_b.append({\"x\": cx, \"y\": cy})\ndf_boxes_a = pd.DataFrame(boxes_a)\ndf_boxes_b = pd.DataFrame(boxes_b)\n\n# Thin grid lines (cell divisions only — skips 3×3 box positions)\nthin_lines = []\nfor i in range(1, 9):\n    if i % 3 != 0:\n        thin_lines.append({\"x\": i, \"xend\": i, \"y\": 0, \"yend\": 9})\n        thin_lines.append({\"x\": 0, \"xend\": 9, \"y\": i, \"yend\": i})\ndf_thin = pd.DataFrame(thin_lines)\n\n# Interior box boundary lines (3×3 separators, not the outer border)\nbox_lines = []\nfor i in [3, 6]:\n    box_lines.append({\"x\": i, \"xend\": i, \"y\": 0, \"yend\": 9})\n    box_lines.append({\"x\": 0, \"xend\": 9, \"y\": i, \"yend\": i})\ndf_box = pd.DataFrame(box_lines)\n\n# Outer border — slightly thicker than box lines for three-level visual hierarchy\nouter_border = [\n    {\"x\": 0, \"xend\": 9, \"y\": 0, \"yend\": 0},\n    {\"x\": 0, \"xend\": 9, \"y\": 9, \"yend\": 9},\n    {\"x\": 0, \"xend\": 0, \"y\": 0, \"yend\": 9},\n    {\"x\": 9, \"xend\": 9, \"y\": 0, \"yend\": 9},\n]\ndf_outer = pd.DataFrame(outer_border)\n\n# Plot\ntitle = \"sudoku-basic · python · plotnine · anyplot.ai\"\nplot = (\n    ggplot()\n    + geom_tile(data=df_boxes_a, mapping=aes(x=\"x\", y=\"y\"), fill=PAGE_BG, color=\"none\", width=3, height=3)\n    + geom_tile(data=df_boxes_b, mapping=aes(x=\"x\", y=\"y\"), fill=ELEVATED_BG, color=\"none\", width=3, height=3)\n    + geom_segment(\n        data=df_thin, mapping=aes(x=\"x\", y=\"y\", xend=\"xend\", yend=\"yend\"), color=INK_MUTED, size=0.4, alpha=0.35\n    )\n    + geom_segment(data=df_box, mapping=aes(x=\"x\", y=\"y\", xend=\"xend\", yend=\"yend\"), color=INK, size=1.5)\n    + geom_segment(data=df_outer, mapping=aes(x=\"x\", y=\"y\", xend=\"xend\", yend=\"yend\"), color=INK, size=2.5)\n    + geom_text(data=df_cells, mapping=aes(x=\"x\", y=\"y\", label=\"value\"), size=13, color=INK, fontweight=\"bold\")\n    + labs(title=title)\n    + coord_fixed(ratio=1, xlim=(-0.05, 9.05), ylim=(-0.05, 9.05))\n    + theme_void()\n    + theme(\n        figure_size=(6, 6),\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=12, ha=\"center\", weight=\"bold\", color=INK, margin={\"b\": 10}),\n        plot_margin=0.04,\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=6, height=6, units=\"in\", verbose=False)\n"}