{"spec_id":"qrcode-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nqrcode-basic: Basic QR Code Generator\nLibrary: matplotlib 3.11.0 | Python 3.13.14\nQuality: 91/100 | Updated: 2026-06-24\n\"\"\"\n\nimport os\nimport sys\n\n\n# This file is named matplotlib.py. Remove its directory from sys.path to\n# prevent it from shadowing the matplotlib package during import.\n_this_dir = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if os.path.abspath(p or \".\") != _this_dir]\n\nimport matplotlib.patches as mpatches\nimport matplotlib.patheffects as pe\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport qrcode\nfrom matplotlib.colors import LinearSegmentedColormap, ListedColormap\n\n\n# Theme-adaptive chrome 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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint palette — first categorical series always #009E73\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\nBRAND = IMPRINT_PALETTE[0]  # #009E73\n\n# Data — generate a real, scannable QR code encoding URL\ncontent = \"https://anyplot.ai\"\nqr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=1, border=4)\nqr.add_data(content)\nqr.make(fit=True)\nqr_matrix = np.array(qr.get_matrix(), dtype=int)\nrows, cols = qr_matrix.shape  # includes 4-module quiet zone border on each side\n\n# Square canvas: 2400×2400 px — QR code is inherently square\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# QR modules: always dark on light background for reliable scanner recognition\nQR_CARD = \"#FAF8F1\"  # cream card, scannable in both light and dark themes\nQR_MOD = \"#1A1A17\"  # dark modules, high contrast\nqr_cmap = ListedColormap([QR_CARD, QR_MOD])\nax.imshow(qr_matrix, cmap=qr_cmap, interpolation=\"nearest\", vmin=0, vmax=1, aspect=\"equal\", zorder=2)\nax.set_axis_off()\n\n# Axes limits: margin provides space for the decorative frame\nmargin = 2.5\nax.set_xlim(-margin, cols - 1 + margin)\nax.set_ylim(rows - 1 + margin, -margin)  # inverted y so row-0 appears at top\n\n# Rounded card background (keeps QR on light surface even in dark theme)\ncp = 0.7  # card padding around QR module boundary\ncard = mpatches.FancyBboxPatch(\n    (-0.5 - cp, -0.5 - cp),\n    cols + 2 * cp,\n    rows + 2 * cp,\n    boxstyle=mpatches.BoxStyle.Round(pad=0.35, rounding_size=0.9),\n    facecolor=QR_CARD,\n    edgecolor=BRAND,\n    linewidth=2.5,\n    zorder=1,\n)\nax.add_patch(card)\n\n# Shadow for visual depth (behind card)\nshadow = mpatches.FancyBboxPatch(\n    (-0.5 - cp + 0.3, -0.5 - cp + 0.3),\n    cols + 2 * cp,\n    rows + 2 * cp,\n    boxstyle=mpatches.BoxStyle.Round(pad=0.35, rounding_size=0.9),\n    facecolor=\"none\",\n    edgecolor=BRAND,\n    linewidth=2.5,\n    alpha=0.15,\n    zorder=0,\n)\nax.add_patch(shadow)\n\n# Highlight the 3 Position Detection (finder) patterns with BRAND green outlines\n# QR v2: 25×25 data area + 4-module quiet zone → 33×33 total matrix\n# Finder centers in data coords (x=col, y=row):\n#   top-left (bsz+3, bsz+3), top-right (cols-1-bsz-3, bsz+3), bottom-left (bsz+3, rows-1-bsz-3)\nbsz = qr.border  # quiet zone border width in modules\nfhalf = 3.5  # half of 7-module finder pattern width\n\nfinder_centers = [\n    (bsz + 3, bsz + 3),  # top-left\n    (cols - 1 - bsz - 3, bsz + 3),  # top-right\n    (bsz + 3, rows - 1 - bsz - 3),  # bottom-left\n]\nfor fx, fy in finder_centers:\n    ax.add_patch(\n        mpatches.FancyBboxPatch(\n            (fx - fhalf, fy - fhalf),\n            7,\n            7,\n            boxstyle=mpatches.BoxStyle.Round(pad=0.2, rounding_size=0.4),\n            facecolor=\"none\",\n            edgecolor=BRAND,\n            linewidth=1.5,\n            alpha=0.75,\n            zorder=3,\n        )\n    )\n\n# Title with subtle PathEffects glow\ntitle_str = \"qrcode-basic · python · matplotlib · anyplot.ai\"\ntitle = ax.set_title(title_str, fontsize=11, fontweight=\"medium\", color=INK, pad=10)\ntitle.set_path_effects([pe.withStroke(linewidth=2.5, foreground=PAGE_BG), pe.Normal()])\n\n# Gradient accent divider: Imprint palette brand green → blue\nimprint_grad = LinearSegmentedColormap.from_list(\"imprint_grad\", [BRAND, IMPRINT_PALETTE[2]])\naccent_ax = fig.add_axes([0.15, 0.116, 0.70, 0.0018])\naccent_ax.imshow(np.linspace(0, 1, 256).reshape(1, -1), aspect=\"auto\", cmap=imprint_grad, extent=[0, 1, 0, 1])\naccent_ax.set_axis_off()\n\n# Footer: encoded URL (monospace) and QR structural metadata\nencoded_t = fig.text(0.5, 0.083, f\"Encoded: {content}\", ha=\"center\", fontsize=9, color=INK_SOFT, family=\"monospace\")\nencoded_t.set_path_effects([pe.withStroke(linewidth=1.5, foreground=PAGE_BG), pe.Normal()])\n\nmeta_str = (\n    f\"Version {qr.version}  ·  {rows}×{cols} modules  ·  \"\n    f\"Error Correction M (15%)  ·  3 position detection patterns highlighted\"\n)\nmeta_t = fig.text(0.5, 0.047, meta_str, ha=\"center\", fontsize=7.5, color=INK_MUTED, style=\"italic\")\nmeta_t.set_path_effects([pe.withStroke(linewidth=1.2, foreground=PAGE_BG), pe.Normal()])\n\nfig.subplots_adjust(left=0.08, right=0.92, top=0.91, bottom=0.14)\n\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}