{"spec_id":"datamatrix-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\ndatamatrix-basic: Basic Data Matrix 2D Barcode\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 87/100 | Updated: 2026-05-20\n\"\"\"\n\nimport importlib\nimport os\nimport sys\n\n\n# This file is named matplotlib.py, which shadows the installed matplotlib package.\n# Remove the script directory from sys.path so Python finds the real package.\n_here = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p or os.getcwd()) != _here]\ndel _here\n\nplt = importlib.import_module(\"matplotlib.pyplot\")\nmpatches = importlib.import_module(\"matplotlib.patches\")\nnp = importlib.import_module(\"numpy\")\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Zone colors — Okabe-Ito, theme-constant (data encoding)\nCOLOR_FINDER = \"#009E73\"\nCOLOR_TIMING = \"#4467A3\"\nCOLOR_DATA = \"#AE3030\"\n\n# Data — 20×20 Data Matrix for electronics component traceability\nnp.random.seed(42)\nSIZE = 20\ncontent = \"PCB:2026-A3F7\"\n\n# Initialize matrix (0 = white, 1 = black)\nmatrix = np.zeros((SIZE, SIZE), dtype=int)\n\n# L-shaped finder pattern: solid black left column + bottom row\nmatrix[:, 0] = 1\nmatrix[-1, :] = 1\n\n# Alternating timing pattern on top row + right column\nfor i in range(SIZE):\n    matrix[0, i] = i % 2  # top row: alternating, starts white\n    matrix[i, -1] = (i + 1) % 2  # right col: alternating, starts black\n\n# Interior data: deterministic pseudo-random seeded from content bytes\ncontent_seed = sum(ord(c) for c in content)\ninterior = np.random.RandomState(content_seed).randint(0, 2, size=(SIZE - 2, SIZE - 2))\nmatrix[1:-1, 1:-1] = interior\n\n# Canvas — 2800×2800 px (wider quiet zone to accommodate zone callout annotations)\nfig, ax = plt.subplots(figsize=(7, 7), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(\"white\")  # barcode scanning standard requires white cell background\n\n# Render Data Matrix with pcolormesh for crisp pixel-grid cell boundaries\nxc = np.arange(SIZE + 1)\nyc = np.arange(SIZE + 1)\nax.pcolormesh(xc, yc, matrix[::-1], cmap=\"binary\", edgecolors=\"none\", linewidth=0, zorder=5)\n\nax.set_aspect(\"equal\")\nqz = 3.0\nax.set_xlim(-qz, SIZE + qz)\nax.set_ylim(-qz, SIZE + qz)\nax.axis(\"off\")\n\n# ── Structural zone overlays (matplotlib.patches.Rectangle) ───────────────\n# After matrix[::-1] flip: y=0 (bottom) = L-finder row; y=SIZE-1 (top) = timing row\nax.add_patch(mpatches.Rectangle((0, 0), 1, SIZE, fc=COLOR_FINDER, alpha=0.22, ec=\"none\", zorder=6))\nax.add_patch(mpatches.Rectangle((0, 0), SIZE, 1, fc=COLOR_FINDER, alpha=0.22, ec=\"none\", zorder=6))\nax.add_patch(mpatches.Rectangle((0, SIZE - 1), SIZE, 1, fc=COLOR_TIMING, alpha=0.22, ec=\"none\", zorder=6))\nax.add_patch(mpatches.Rectangle((SIZE - 1, 0), 1, SIZE, fc=COLOR_TIMING, alpha=0.22, ec=\"none\", zorder=6))\nax.add_patch(mpatches.Rectangle((1, 1), SIZE - 2, SIZE - 2, fc=COLOR_DATA, alpha=0.07, ec=\"none\", zorder=4))\n# Quiet-zone border outlines the scanning area\nax.add_patch(mpatches.Rectangle((0, 0), SIZE, SIZE, fc=\"none\", ec=INK_MUTED, lw=0.4, zorder=7))\n\n# ── Zone callout annotations with arrows ──────────────────────────────────\nax.annotate(\n    \"L-finder\\n(solid)\",\n    xy=(0.5, SIZE / 2),\n    xytext=(-1.8, SIZE / 2),\n    ha=\"right\",\n    va=\"center\",\n    fontsize=7.5,\n    color=COLOR_FINDER,\n    arrowprops={\"arrowstyle\": \"->\", \"color\": COLOR_FINDER, \"lw\": 0.9, \"mutation_scale\": 9},\n)\nax.annotate(\n    \"L-finder\\n(solid)\",\n    xy=(SIZE / 2, 0.5),\n    xytext=(SIZE / 2, -1.8),\n    ha=\"center\",\n    va=\"top\",\n    fontsize=7.5,\n    color=COLOR_FINDER,\n    arrowprops={\"arrowstyle\": \"->\", \"color\": COLOR_FINDER, \"lw\": 0.9, \"mutation_scale\": 9},\n)\nax.annotate(\n    \"Timing strip\\n(alternating)\",\n    xy=(SIZE / 2, SIZE - 0.5),\n    xytext=(SIZE / 2, SIZE + 1.8),\n    ha=\"center\",\n    va=\"bottom\",\n    fontsize=7.5,\n    color=COLOR_TIMING,\n    arrowprops={\"arrowstyle\": \"->\", \"color\": COLOR_TIMING, \"lw\": 0.9, \"mutation_scale\": 9},\n)\nax.annotate(\n    \"Timing strip\\n(alternating)\",\n    xy=(SIZE - 0.5, SIZE / 2),\n    xytext=(SIZE + 1.8, SIZE / 2),\n    ha=\"left\",\n    va=\"center\",\n    fontsize=7.5,\n    color=COLOR_TIMING,\n    arrowprops={\"arrowstyle\": \"->\", \"color\": COLOR_TIMING, \"lw\": 0.9, \"mutation_scale\": 9},\n)\n# ECC 200 data region label — center of interior\nax.annotate(\n    \"ECC 200\\ndata region\",\n    xy=(SIZE / 2, SIZE / 2),\n    xytext=(SIZE / 2, SIZE / 2),\n    ha=\"center\",\n    va=\"center\",\n    fontsize=7,\n    color=COLOR_DATA,\n    alpha=0.65,\n    arrowprops=None,\n)\n\n# Title\nax.set_title(\"datamatrix-basic · python · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, pad=14)\n\n# Encoded content annotation\nfig.text(\n    0.5,\n    0.055,\n    f\"Data Matrix 20×20  ·  Encoded: {content}  ·  ECC 200\",\n    ha=\"center\",\n    va=\"bottom\",\n    fontsize=9,\n    fontfamily=\"monospace\",\n    color=INK_SOFT,\n)\n\nplt.tight_layout(rect=[0, 0.08, 1, 1])\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}