{"spec_id":"chessboard-pieces","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nchessboard-pieces: Chess Board with Pieces for Position Diagrams\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\n\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    coord_fixed,\n    element_blank,\n    element_rect,\n    element_text,\n    geom_text,\n    geom_tile,\n    ggplot,\n    labs,\n    scale_fill_manual,\n    scale_x_continuous,\n    scale_y_continuous,\n    theme,\n)\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Unicode chess symbols mapping\nUNICODE_PIECES = {\n    \"K\": \"♔\",  # White King\n    \"Q\": \"♕\",  # White Queen\n    \"R\": \"♖\",  # White Rook\n    \"B\": \"♗\",  # White Bishop\n    \"N\": \"♘\",  # White Knight\n    \"P\": \"♙\",  # White Pawn\n    \"k\": \"♚\",  # Black King\n    \"q\": \"♛\",  # Black Queen\n    \"r\": \"♜\",  # Black Rook\n    \"b\": \"♝\",  # Black Bishop\n    \"n\": \"♞\",  # Black Knight\n    \"p\": \"♟\",  # Black Pawn\n}\n\n# Scholar's Mate position - a famous 4-move checkmate\npieces = {\n    # White pieces\n    \"a1\": \"R\",\n    \"b1\": \"N\",\n    \"c1\": \"B\",\n    \"d1\": \"Q\",\n    \"e1\": \"K\",\n    \"h1\": \"R\",\n    \"a2\": \"P\",\n    \"b2\": \"P\",\n    \"c2\": \"P\",\n    \"d2\": \"P\",\n    \"f2\": \"P\",\n    \"g2\": \"P\",\n    \"h2\": \"P\",\n    \"f3\": \"N\",\n    \"c4\": \"B\",\n    \"f7\": \"Q\",  # White Queen delivers checkmate\n    # Black pieces\n    \"a8\": \"r\",\n    \"b8\": \"n\",\n    \"c8\": \"b\",\n    \"d8\": \"q\",\n    \"f8\": \"b\",\n    \"g8\": \"n\",\n    \"h8\": \"r\",\n    \"a7\": \"p\",\n    \"b7\": \"p\",\n    \"c7\": \"p\",\n    \"d7\": \"p\",\n    \"g7\": \"p\",\n    \"h7\": \"p\",\n    \"e5\": \"p\",\n    \"e8\": \"k\",  # Black King in checkmate\n}\n\n# Create board data\nfiles = list(\"abcdefgh\")\nranks = list(range(1, 9))\n\nboard_data = []\nfor file_idx, file in enumerate(files):\n    for rank in ranks:\n        square = f\"{file}{rank}\"\n        is_light = (file_idx + rank) % 2 == 1\n        color = \"light\" if is_light else \"dark\"\n        board_data.append({\"file\": file_idx + 1, \"rank\": rank, \"color\": color, \"square\": square})\n\nboard_df = pd.DataFrame(board_data)\n\n# Create pieces data\npieces_data = []\nfor square, piece in pieces.items():\n    file = files.index(square[0]) + 1\n    rank = int(square[1])\n    unicode_piece = UNICODE_PIECES.get(piece, \"\")\n    pieces_data.append({\"file\": file, \"rank\": rank, \"piece\": unicode_piece})\n\npieces_df = pd.DataFrame(pieces_data)\n\n# Board colors (theme-independent - chess tradition)\nboard_colors = {\"light\": \"#F0D9B5\", \"dark\": \"#B58863\"}\n\n# Piece color adapts to theme for visibility\npiece_color = \"#000000\" if THEME == \"light\" else \"#FFFFFF\"\n\n# Create the plot\nplot = (\n    ggplot()\n    + geom_tile(data=board_df, mapping=aes(x=\"file\", y=\"rank\", fill=\"color\"), width=1, height=1)\n    + scale_fill_manual(values=board_colors)\n    + geom_text(\n        data=pieces_df, mapping=aes(x=\"file\", y=\"rank\", label=\"piece\"), size=28, family=\"DejaVu Sans\", color=piece_color\n    )\n    + scale_x_continuous(breaks=list(range(1, 9)), labels=list(\"abcdefgh\"), expand=(0, 0))\n    + scale_y_continuous(breaks=list(range(1, 9)), labels=[str(i) for i in range(1, 9)], expand=(0, 0))\n    + coord_fixed(ratio=1)\n    + labs(title=\"Scholar's Mate · chessboard-pieces · plotnine · anyplot.ai\")\n    + theme(\n        figure_size=(12, 12),\n        plot_title=element_text(size=24, ha=\"center\", weight=\"bold\", color=INK),\n        axis_title=element_blank(),\n        axis_text_x=element_text(size=20, weight=\"bold\", color=INK_SOFT),\n        axis_text_y=element_text(size=20, weight=\"bold\", color=INK_SOFT),\n        axis_ticks=element_blank(),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_blank(),\n        panel_grid_minor=element_blank(),\n        plot_background=element_rect(fill=PAGE_BG),\n        legend_position=\"none\",\n    )\n)\n\nplot.save(f\"plot-{THEME}.png\", dpi=300, width=12, height=12)\n"}