{"spec_id":"chessboard-pieces","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nchessboard-pieces: Chess Board with Pieces for Position Diagrams\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 85/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\nimport sys\nfrom pathlib import Path\n\n\n# Avoid module shadowing: remove the script's directory from sys.path before importing\nscript_dir = Path(__file__).parent.resolve()\noriginal_path = sys.path.copy()\nsys.path = [p for p in sys.path if Path(p).resolve() != script_dir and p not in (\"\", \".\")]\n\nimport matplotlib.patheffects as pe\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\n\n\nsys.path = original_path\ndel original_path\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_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Chess piece Unicode symbols\nPIECE_SYMBOLS = {\n    \"K\": \"♔\",\n    \"Q\": \"♕\",\n    \"R\": \"♖\",\n    \"B\": \"♗\",\n    \"N\": \"♘\",\n    \"P\": \"♙\",  # White pieces\n    \"k\": \"♚\",\n    \"q\": \"♛\",\n    \"r\": \"♜\",\n    \"b\": \"♝\",\n    \"n\": \"♞\",\n    \"p\": \"♟\",  # Black pieces\n}\n\n# Italian Game position: After 1.e4 e5 2.Nf3 Nc6 3.Bc4 Nf6 4.Ng5 d5 5.exd5 Nxd5\n# This is the Fried Liver starting position - famous and educational\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    \"c4\": \"B\",\n    \"f3\": \"N\",\n    \"g5\": \"N\",\n    # Black pieces\n    \"a8\": \"r\",\n    \"b8\": \"n\",\n    \"c8\": \"b\",\n    \"d8\": \"q\",\n    \"e8\": \"k\",\n    \"f8\": \"b\",\n    \"h8\": \"r\",\n    \"a7\": \"p\",\n    \"b7\": \"p\",\n    \"c7\": \"p\",\n    \"e7\": \"p\",\n    \"f7\": \"p\",\n    \"g7\": \"p\",\n    \"h7\": \"p\",\n    \"d5\": \"n\",\n    \"f6\": \"n\",\n    \"c6\": \"n\",\n}\n\n# Create board data for heatmap\n# h1 should be light (chess standard), so pattern needs adjustment\nboard_colors = np.zeros((8, 8))\nfor row in range(8):\n    for col in range(8):\n        # Checkerboard pattern: h1 (row=7, col=7) should be light (0)\n        board_colors[row, col] = (row + col + 1) % 2\n\n# Set up seaborn theme with theme-adaptive colors\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n    },\n)\n\n# Create figure with square aspect ratio for chessboard\nfig, ax = plt.subplots(figsize=(12, 12), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Use seaborn heatmap for the board squares\nsns.heatmap(\n    board_colors,\n    ax=ax,\n    cmap=[\"#F0D9B5\", \"#B58863\"],  # Classic chess board colors\n    cbar=False,\n    square=True,\n    linewidths=0.5,\n    linecolor=\"#8B7355\",\n    xticklabels=list(\"abcdefgh\"),\n    yticklabels=list(\"87654321\"),\n)\n\n# Style tick labels with theme-adaptive colors\nax.tick_params(\n    axis=\"both\",\n    which=\"both\",\n    length=0,\n    labelsize=20,\n    labelbottom=True,\n    labeltop=False,\n    labelleft=True,\n    labelright=False,\n    colors=INK_SOFT,\n)\nax.set_xticklabels(list(\"abcdefgh\"), fontsize=20, fontweight=\"bold\", color=INK_SOFT)\nax.set_yticklabels(list(\"87654321\"), fontsize=20, fontweight=\"bold\", color=INK_SOFT)\n\n# Place pieces on the board\nfor square, piece in pieces.items():\n    col = ord(square[0]) - ord(\"a\")  # a=0, b=1, ..., h=7\n    row = 8 - int(square[1])  # 8=0, 7=1, ..., 1=7\n\n    symbol = PIECE_SYMBOLS.get(piece, \"\")\n    # White pieces in white, black pieces in dark\n    piece_color = INK if piece.islower() else \"#ffffff\"\n    # Outline color adjusted for theme\n    outline_color = INK_SOFT if THEME == \"light\" else \"#4A4A44\"\n    # Add path effect for visibility\n    ax.text(\n        col + 0.5,\n        row + 0.5,\n        symbol,\n        fontsize=48,\n        ha=\"center\",\n        va=\"center\",\n        color=piece_color,\n        fontweight=\"bold\",\n        path_effects=[pe.withStroke(linewidth=2, foreground=outline_color)],\n    )\n\n# Title with theme-adaptive color\nax.set_title(\n    \"Italian Game · chessboard-pieces · seaborn · anyplot.ai\", fontsize=24, fontweight=\"bold\", pad=20, color=INK\n)\n\n# Remove axis labels\nax.set_xlabel(\"\")\nax.set_ylabel(\"\")\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}