{"spec_id":"eye-diagram-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\neye-diagram-basic: Signal Integrity Eye Diagram\nLibrary: matplotlib 3.11.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-06-18\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import LinearSegmentedColormap\nfrom scipy.ndimage import gaussian_filter\n\n\n# Theme tokens (Imprint palette, theme-adaptive 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\"\n\n# Imprint sequential colormap — 3-stop green→cyan→blue for richer visual depth\nimprint_seq = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#2ABCCD\", \"#4467A3\"])\nimprint_seq.set_bad(color=PAGE_BG)\n\n# Data — simulated NRZ signal with controlled noise and jitter\nnp.random.seed(42)\n\nn_traces = 400\nsamples_per_ui = 150\nn_bits = 3\nnoise_sigma = 0.05\njitter_sigma = 0.03\n\nbit_sequences = np.random.randint(0, 2, (n_traces, n_bits + 2))\n\nall_time = []\nall_voltage = []\n\nfor i in range(n_traces):\n    bits = bit_sequences[i]\n    t_full = np.linspace(-1, n_bits + 1, (n_bits + 2) * samples_per_ui)\n    signal = np.zeros_like(t_full)\n\n    for b in range(n_bits + 2):\n        signal += bits[b] * (1 / (1 + np.exp(-20 * (t_full - b + 0.5))))\n        if b > 0:\n            signal -= bits[b - 1] * (1 / (1 + np.exp(-20 * (t_full - b + 0.5))))\n\n    t_jittered = t_full + np.random.normal(0, jitter_sigma, len(t_full))\n    noise = np.random.normal(0, noise_sigma, len(t_full))\n    signal_noisy = signal + noise\n\n    mask = (t_jittered >= 0) & (t_jittered <= 2)\n    all_time.extend(t_jittered[mask])\n    all_voltage.extend(signal_noisy[mask])\n\nall_time = np.array(all_time)\nall_voltage = np.array(all_voltage)\n\n# 2D density histogram with Gaussian smoothing for crisp rendering\nh, xedges, yedges = np.histogram2d(all_time, all_voltage, bins=[500, 340], range=[[0, 2], [-0.3, 1.3]])\nh_smooth = gaussian_filter(np.log1p(h.T), sigma=1.2)\nh_masked = np.ma.masked_where(h_smooth < 0.01, h_smooth)\n\n# Eye measurements — 2-sigma approximation from simulation parameters\neye_height = 1.0 - 4 * noise_sigma  # ~0.80V\neye_width = 1.0 - 4 * jitter_sigma  # ~0.88 UI\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nim = ax.imshow(\n    h_masked, origin=\"lower\", aspect=\"auto\", extent=[0, 2, -0.3, 1.3], cmap=imprint_seq, interpolation=\"bilinear\"\n)\n\n# Nominal signal level reference lines at 0 V and 1 V\nfor ref_v in (0.0, 1.0):\n    ax.axhline(ref_v, color=INK_SOFT, linestyle=\"--\", linewidth=0.8, alpha=0.40)\n\n# Faint y-axis grid to aid voltage estimation (drawn above heatmap)\nax.yaxis.grid(True, alpha=0.10, linewidth=0.6, color=INK)\n\n# Eye height annotation — vertical arrow in the second eye opening (shifted left from edge)\nt_annot = 1.62\nax.annotate(\n    \"\", xy=(t_annot, 0.10), xytext=(t_annot, 0.90), arrowprops={\"arrowstyle\": \"<->\", \"color\": INK_SOFT, \"lw\": 1.2}\n)\nax.text(\n    1.67,\n    0.50,\n    f\"Eye H = {eye_height:.2f}V\",\n    fontsize=7,\n    color=INK_SOFT,\n    va=\"center\",\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.85, \"boxstyle\": \"round,pad=0.2\"},\n)\n\n# Eye width annotation — horizontal arrow across second eye opening at mid-voltage\nt_left = 1.0 + (1.0 - eye_width) / 2\nt_right = 2.0 - (1.0 - eye_width) / 2\nax.annotate(\n    \"\", xy=(t_left, 0.44), xytext=(t_right, 0.44), arrowprops={\"arrowstyle\": \"<->\", \"color\": INK_SOFT, \"lw\": 1.2}\n)\nax.text(\n    1.50,\n    0.36,\n    f\"Eye W = {eye_width:.2f} UI\",\n    fontsize=7,\n    color=INK_SOFT,\n    ha=\"center\",\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.85, \"boxstyle\": \"round,pad=0.2\"},\n)\n\n# Colorbar — trace density scale (primary matplotlib heatmap feature)\ncbar = fig.colorbar(im, ax=ax, shrink=0.75, pad=0.02, aspect=25)\ncbar.set_label(\"Trace density (log)\", fontsize=8, color=INK_SOFT)\ncbar.ax.tick_params(labelsize=7, colors=INK_SOFT, labelcolor=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\ncbar.ax.set_facecolor(PAGE_BG)\n\n# Style\ntitle = \"eye-diagram-basic · python · matplotlib · anyplot.ai\"\nax.set_xlabel(\"Time (UI)\", fontsize=10, color=INK)\nax.set_ylabel(\"Voltage (V)\", fontsize=10, color=INK)\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\n\nax.set_xticks([0, 0.5, 1.0, 1.5, 2.0])\nax.set_yticks([0, 0.25, 0.5, 0.75, 1.0])\n\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\n\nplt.tight_layout()\n\n# Save — no bbox_inches to preserve exact 3200×1800 canvas\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}