{"spec_id":"barcode-ean13","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nbarcode-ean13: EAN-13 Barcode\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 86/100 | Updated: 2026-05-21\n\"\"\"\n\nimport os\n\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    coord_cartesian,\n    element_rect,\n    element_text,\n    geom_rect,\n    geom_text,\n    ggplot,\n    labs,\n    theme,\n    theme_void,\n)\n\n\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\n\n# EAN-13 encoding patterns\nL_CODES = {\n    \"0\": \"0001101\",\n    \"1\": \"0011001\",\n    \"2\": \"0010011\",\n    \"3\": \"0111101\",\n    \"4\": \"0100011\",\n    \"5\": \"0110001\",\n    \"6\": \"0101111\",\n    \"7\": \"0111011\",\n    \"8\": \"0110111\",\n    \"9\": \"0001011\",\n}\nG_CODES = {\n    \"0\": \"0100111\",\n    \"1\": \"0110011\",\n    \"2\": \"0011011\",\n    \"3\": \"0100001\",\n    \"4\": \"0011101\",\n    \"5\": \"0111001\",\n    \"6\": \"0000101\",\n    \"7\": \"0010001\",\n    \"8\": \"0001001\",\n    \"9\": \"0010111\",\n}\nR_CODES = {\n    \"0\": \"1110010\",\n    \"1\": \"1100110\",\n    \"2\": \"1101100\",\n    \"3\": \"1000010\",\n    \"4\": \"1011100\",\n    \"5\": \"1001110\",\n    \"6\": \"1010000\",\n    \"7\": \"1000100\",\n    \"8\": \"1001000\",\n    \"9\": \"1110100\",\n}\nFIRST_DIGIT_PATTERN = {\n    \"0\": \"LLLLLL\",\n    \"1\": \"LLGLGG\",\n    \"2\": \"LLGGLG\",\n    \"3\": \"LLGGGL\",\n    \"4\": \"LGLLGG\",\n    \"5\": \"LGGLLG\",\n    \"6\": \"LGGGLL\",\n    \"7\": \"LGLGLG\",\n    \"8\": \"LGLGGL\",\n    \"9\": \"LGGLGL\",\n}\n\n# Data: German product EAN-13 code\ncode = \"4006381333931\"\n\nif len(code) == 12:\n    odd_sum = sum(int(code[i]) for i in range(0, 12, 2))\n    even_sum = sum(int(code[i]) for i in range(1, 12, 2))\n    check = (10 - (odd_sum + even_sum * 3) % 10) % 10\n    code = code + str(check)\n\nfirst_digit = code[0]\nleft_digits = code[1:7]\nright_digits = code[7:13]\nparity_pattern = FIRST_DIGIT_PATTERN[first_digit]\n\nbinary = \"101\"\nfor i, digit in enumerate(left_digits):\n    binary += L_CODES[digit] if parity_pattern[i] == \"L\" else G_CODES[digit]\nbinary += \"01010\"\nfor digit in right_digits:\n    binary += R_CODES[digit]\nbinary += \"101\"\n\n# Build bar rectangles (guards extend to ymin=0, data bars start at ymin=5)\nquiet_zone = 9\nbars = []\nx_pos = quiet_zone\nfor i, bit in enumerate(binary):\n    if bit == \"1\":\n        is_guard = i < 3 or i >= len(binary) - 3 or (45 <= i < 50)\n        bars.append({\"xmin\": x_pos, \"xmax\": x_pos + 1, \"ymin\": 0 if is_guard else 5, \"ymax\": 70})\n    x_pos += 1\n\ndf_bars = pd.DataFrame(bars)\ntotal_width = quiet_zone * 2 + len(binary)\n\n# Digit label positions\ndigit_labels = [{\"x\": quiet_zone - 4, \"y\": -8, \"label\": first_digit}]\nleft_start = quiet_zone + 3\nfor i, digit in enumerate(left_digits):\n    digit_labels.append({\"x\": left_start + i * 7 + 3.5, \"y\": -8, \"label\": digit})\nright_start = quiet_zone + 3 + 42 + 5\nfor i, digit in enumerate(right_digits):\n    digit_labels.append({\"x\": right_start + i * 7 + 3.5, \"y\": -8, \"label\": digit})\n\ndf_labels = pd.DataFrame(digit_labels)\n\n# Plot\nplot = (\n    ggplot()\n    + geom_rect(df_bars, aes(xmin=\"xmin\", xmax=\"xmax\", ymin=\"ymin\", ymax=\"ymax\"), fill=INK, color=INK)\n    + geom_text(df_labels, aes(x=\"x\", y=\"y\", label=\"label\"), size=14, color=INK, family=\"monospace\", fontweight=\"bold\")\n    + labs(title=\"barcode-ean13 · python · plotnine · anyplot.ai\")\n    + coord_cartesian(xlim=(-2, total_width + 2), ylim=(-14, 74))\n    + theme_void()\n    + theme(\n        figure_size=(8, 4.5),\n        plot_title=element_text(size=12, color=INK, ha=\"center\", margin={\"t\": 6, \"b\": 2}),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n    )\n)\n\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\")\n"}