{"spec_id":"chessboard-pieces","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nchessboard-pieces: Chess Board with Pieces for Position Diagrams\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\nimport sys\n\n\n# Workaround for module name conflict: ensure we import the actual matplotlib package\n# by removing the current directory from sys.path temporarily\n_original_path = sys.path[:]\n_remove_items = [\"\", \".\", os.path.dirname(__file__)]\nsys.path = [p for p in sys.path if p not in _remove_items]\n\nimport matplotlib.patches as patches\nimport matplotlib.pyplot as plt\n\n\nsys.path = _original_path\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\"\nHIGHLIGHT = \"#FFD700\" if THEME == \"light\" else \"#FFD700\"  # Subtle gold for mate threat\n\n# Unicode chess symbols\nPIECE_SYMBOLS = {\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 quick checkmate\n# After 1.e4 e5 2.Bc4 Nc6 3.Qh5 Nf6?? 4.Qxf7#\npieces = {\n    # White pieces\n    \"a1\": \"R\",\n    \"b1\": \"N\",\n    \"c1\": \"B\",\n    \"d1\": \"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    \"c4\": \"B\",\n    \"e4\": \"P\",\n    \"f7\": \"Q\",\n    # Black pieces\n    \"a8\": \"r\",\n    \"b8\": \"n\",\n    \"c8\": \"b\",\n    \"d8\": \"q\",\n    \"e8\": \"k\",\n    \"h8\": \"r\",\n    \"a7\": \"p\",\n    \"b7\": \"p\",\n    \"c7\": \"p\",\n    \"d7\": \"p\",\n    \"g7\": \"p\",\n    \"h7\": \"p\",\n    \"c6\": \"n\",\n    \"e5\": \"p\",\n    \"f6\": \"n\",\n}\n\n# Create figure (square format for chessboard)\nfig, ax = plt.subplots(figsize=(12, 12), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Board colors (elegant chess board)\nlight_color = \"#F0D9B5\"  # Light squares\ndark_color = \"#B58863\"  # Dark squares\n\n# Draw board squares using patch collection for efficiency\nsquares = []\ncolors = []\nfor row in range(8):\n    for col in range(8):\n        color = light_color if (col + row) % 2 == 1 else dark_color\n        rect = patches.Rectangle((col, row), 1, 1, linewidth=0, facecolor=color)\n        ax.add_patch(rect)\n\n# Highlight the mate threat (f7 - where white queen delivers checkmate)\nmate_col, mate_row = 5, 6  # f7 in 0-indexed coordinates\nhighlight = patches.Rectangle((mate_col, mate_row), 1, 1, linewidth=0, facecolor=HIGHLIGHT, alpha=0.15, zorder=2)\nax.add_patch(highlight)\n\n# Draw pieces using text (DejaVu Sans renders Unicode pieces well)\nfor square, piece in pieces.items():\n    col = ord(square[0]) - ord(\"a\")\n    row = int(square[1]) - 1\n    symbol = PIECE_SYMBOLS[piece]\n    ax.text(col + 0.5, row + 0.5, symbol, fontsize=56, ha=\"center\", va=\"center\", fontfamily=\"DejaVu Sans\", zorder=3)\n\n# Add coordinate labels\nfile_labels = \"abcdefgh\"\nrank_labels = \"12345678\"\n\nfor i in range(8):\n    # File labels (a-h) at bottom\n    ax.text(i + 0.5, -0.35, file_labels[i], fontsize=20, ha=\"center\", va=\"center\", fontweight=\"bold\", color=INK)\n    # Rank labels (1-8) on left\n    ax.text(-0.35, i + 0.5, rank_labels[i], fontsize=20, ha=\"center\", va=\"center\", fontweight=\"bold\", color=INK)\n\n# Board border using custom patch\nborder = patches.Rectangle((0, 0), 8, 8, linewidth=4, edgecolor=INK_SOFT, facecolor=\"none\", zorder=4)\nax.add_patch(border)\n\n# Styling\nax.set_xlim(-0.6, 8.2)\nax.set_ylim(-0.6, 8.6)\nax.set_aspect(\"equal\")\nax.axis(\"off\")\n\n# Title\nax.set_title(\"chessboard-pieces · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", pad=20, color=INK)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}