{"spec_id":"qrcode-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nqrcode-basic: Basic QR Code Generator\nLibrary: seaborn 0.13.2 | Python 3.13.14\nQuality: 90/100 | Updated: 2026-06-24\n\"\"\"\n\nimport os\n\nimport matplotlib.colors as mcolors\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport qrcode\nimport seaborn as sns\nfrom matplotlib.colors import ListedColormap\n\n\n# Theme tokens — Imprint palette chrome\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\"\nBRAND = \"#009E73\"  # Imprint palette position 1\n\n# Configure seaborn with theme-adaptive background\nsns.set_theme(\n    style=\"white\",\n    rc={\"figure.facecolor\": PAGE_BG, \"axes.facecolor\": PAGE_BG, \"axes.edgecolor\": \"none\", \"text.color\": INK},\n)\n\n# WiFi credentials QR code (spec §Applications: \"Generating WiFi network credentials\")\n# Version 7 (45×45 modules) showcases alignment patterns alongside the three finder patterns\nencoded_content = \"WIFI:S:AnyPlotLab;T:WPA2;P:OpenDataViz2024!;;\"\nqr = qrcode.QRCode(version=7, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=1, border=4)\nqr.add_data(encoded_content)\nqr.make(fit=False)\n\n# Convert QR matrix to numpy array (includes 4-cell quiet zone on each side)\nqr_matrix = np.array(qr.get_matrix(), dtype=np.uint8)\nborder = 4\nn_modules = qr_matrix.shape[0] - 2 * border  # 45 for version 7\n\n# QR colormap: 0=empty (elevated bg) → 1=filled module (ink) — theme-adaptive\nqr_cmap = ListedColormap([ELEVATED_BG, INK])\n\n# Square canvas: 2400×2400 px (figsize=(6,6) × dpi=400)\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400)\nfig.set_facecolor(PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nsns.heatmap(\n    qr_matrix,\n    ax=ax,\n    cmap=qr_cmap,\n    vmin=0,\n    vmax=1,\n    square=True,\n    cbar=False,\n    xticklabels=False,\n    yticklabels=False,\n    linewidths=0,\n)\n\n# Build region overlay matrix — NaN cells render transparent via seaborn heatmap (masked bad-color)\nfinder_size = 7\nfinder_positions = [\n    (border, border),\n    (border, qr_matrix.shape[1] - border - finder_size),\n    (qr_matrix.shape[0] - border - finder_size, border),\n]\nalign_module_centers = [(6, 22), (22, 6), (22, 22), (22, 38), (38, 22), (38, 38)]\n\nregion_matrix = np.full(qr_matrix.shape, np.nan)\nfor row, col in finder_positions:\n    region_matrix[row : row + finder_size, col : col + finder_size] = 1.0\nfor am_row, am_col in align_module_centers:\n    mr, mc = am_row + border, am_col + border\n    region_matrix[mr - 2 : mr + 3, mc - 2 : mc + 3] = 2.0\n\n\n# Second sns.heatmap overlays region colors using RGBA colors with built-in alpha so\n# set_alpha() on the collection is not needed (it would override NaN transparency)\ndef _rgba(hex_color, alpha):\n    r, g, b = mcolors.to_rgb(hex_color)\n    return (r, g, b, alpha)\n\n\nregion_cmap = ListedColormap([_rgba(BRAND, 0.4), _rgba(INK_SOFT, 0.35)])\nregion_cmap.set_bad(alpha=0)  # NaN cells stay fully transparent\nregion_masked = np.ma.masked_invalid(region_matrix)\nxlim, ylim = ax.get_xlim(), ax.get_ylim()\nsns.heatmap(\n    region_masked,\n    ax=ax,\n    cmap=region_cmap,\n    vmin=0.5,\n    vmax=2.5,\n    cbar=False,\n    xticklabels=False,\n    yticklabels=False,\n    linewidths=0,\n)\nax.set_xlim(xlim)\nax.set_ylim(ylim)\n\n# Remove all chrome for clean QR presentation\nsns.despine(ax=ax, left=True, bottom=True, top=True, right=True)\nax.set_xlabel(\"\")\nax.set_ylabel(\"\")\nax.tick_params(left=False, bottom=False)\n\n# Annotate the top-right finder pattern; label sits in the quiet zone area\nfp_col = qr_matrix.shape[1] - border - finder_size\nfp_row = border\nfp_cx = fp_col + finder_size / 2\nfp_cy = fp_row + finder_size / 2\nax.annotate(\n    \"Finder Pattern\",\n    xy=(fp_cx, fp_cy),\n    xytext=(37, 1.8),\n    fontsize=14,\n    color=BRAND,\n    ha=\"center\",\n    arrowprops={\"arrowstyle\": \"->\", \"color\": BRAND, \"lw\": 1.5},\n)\n\n# Annotate the center alignment pattern — label in bottom quiet zone\nap_mc = 22 + border\nap_mr = 22 + border\nax.annotate(\n    \"Alignment Pattern\",\n    xy=(ap_mc, ap_mr),\n    xytext=(26, 51),\n    fontsize=14,\n    color=INK_SOFT,\n    ha=\"center\",\n    va=\"center\",\n    arrowprops={\"arrowstyle\": \"->\", \"color\": INK_SOFT, \"lw\": 1.2},\n)\n\n# Quiet zone label — rotated text in the left quiet zone\nax.text(\n    1.5,\n    qr_matrix.shape[0] / 2,\n    \"Quiet Zone\",\n    fontsize=14,\n    color=INK_MUTED,\n    rotation=90,\n    ha=\"center\",\n    va=\"center\",\n    fontstyle=\"italic\",\n)\n\n# Title: mandatory format {spec-id} · {language} · {library} · anyplot.ai\ntitle = \"qrcode-basic · python · seaborn · anyplot.ai\"\nax.set_title(title, fontsize=12, fontweight=\"bold\", pad=10, color=INK)\n\n# Subtitle with technical details\nfig.text(\n    0.5,\n    0.025,\n    f\"WiFi: AnyPlotLab (WPA2)  ·  Error Correction: M (15%)  ·  Version {qr.version}  ·  {n_modules}×{n_modules} modules\",\n    ha=\"center\",\n    va=\"bottom\",\n    fontsize=10,\n    color=INK_MUTED,\n    fontstyle=\"italic\",\n)\n\nplt.subplots_adjust(bottom=0.07, top=0.93, left=0.03, right=0.97)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}