{"spec_id":"datamatrix-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\ndatamatrix-basic: Basic Data Matrix 2D Barcode\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 85/100 | Updated: 2026-05-20\n\"\"\"\n\nimport os\nimport sys\n\n\n# Work around filename shadowing the plotnine library\nsys.path.pop(0)\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    coord_fixed,\n    element_rect,\n    element_text,\n    geom_tile,\n    ggplot,\n    labs,\n    scale_fill_manual,\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\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Data - Create a 16x16 Data Matrix barcode encoding \"SERIAL:12345678\"\nnp.random.seed(42)\n\nsize = 16\n\n# Initialize matrix (0 = white, 1 = black)\ndm_matrix = np.zeros((size, size), dtype=int)\n\n# L-shaped finder pattern: solid black left edge and bottom edge\ndm_matrix[:, 0] = 1\ndm_matrix[size - 1, :] = 1\n\n# Alternating clock pattern on top and right edges\nfor i in range(size):\n    dm_matrix[0, i] = i % 2\nfor i in range(size):\n    dm_matrix[i, size - 1] = (size - 1 - i) % 2\n\n# Data encoding area simulating \"SERIAL:12345678\"\ncontent = \"SERIAL:12345678\"\ndata_values = [ord(c) for c in content]\ndata_idx = 0\nfor row in range(1, size - 1):\n    for col in range(1, size - 1):\n        val = data_values[data_idx % len(data_values)]\n        bit_pos = (row + col) % 8\n        dm_matrix[row, col] = (val >> bit_pos) & 1\n        data_idx += 1\n\n# Quiet zone (2-module white border for scanning reliability)\nquiet_zone = 2\npadded_size = size + 2 * quiet_zone\ndm_with_border = np.zeros((padded_size, padded_size), dtype=int)\ndm_with_border[quiet_zone : quiet_zone + size, quiet_zone : quiet_zone + size] = dm_matrix\n\n# Convert matrix to DataFrame (flip y so origin is bottom-left)\nrows = []\nfor row in range(padded_size):\n    for col in range(padded_size):\n        rows.append({\"x\": col, \"y\": padded_size - 1 - row, \"value\": str(dm_with_border[row, col])})\n\ndf = pd.DataFrame(rows)\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"x\", y=\"y\", fill=\"value\"))\n    + geom_tile(color=None, size=0)\n    + scale_fill_manual(values={\"0\": \"#FFFFFF\", \"1\": \"#000000\"})\n    + coord_fixed()\n    + labs(\n        title=\"datamatrix-basic · python · plotnine · anyplot.ai\",\n        subtitle=\"16×16 ECC 200 symbol · 25% error correction\",\n        caption=\"Encoded: SERIAL:12345678\",\n    )\n    + theme_void()\n    + theme(\n        figure_size=(6, 6),\n        plot_title=element_text(size=12, weight=\"bold\", ha=\"center\", color=INK),\n        plot_subtitle=element_text(size=9, ha=\"center\", color=INK_SOFT),\n        plot_caption=element_text(size=9, ha=\"right\", color=INK_SOFT),\n        panel_background=element_rect(fill=PAGE_BG),\n        plot_background=element_rect(fill=PAGE_BG),\n        legend_position=\"none\",\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=6, height=6, units=\"in\")\n"}