{"spec_id":"recurrence-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nrecurrence-basic: Recurrence Plot for Nonlinear Time Series\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-06-10\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport matplotlib.ticker as ticker\nimport numpy as np\nfrom matplotlib.colors import BoundaryNorm, ListedColormap\nfrom scipy.integrate import solve_ivp\nfrom scipy.spatial.distance import cdist\n\n\n# Theme tokens (Imprint palette — see prompts/default-style-guide.md)\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 — always first series\n\n# Data — Lorenz attractor x-component via ODE solver\nsol = solve_ivp(\n    lambda t, s: [10.0 * (s[1] - s[0]), s[0] * (28.0 - s[2]) - s[1], s[0] * s[1] - (8.0 / 3.0) * s[2]],\n    [0, 50],\n    [1.0, 1.0, 1.0],\n    t_eval=np.linspace(0, 50, 5000),\n    max_step=0.01,\n)\nsignal = sol.y[0][::10]  # 500 points from x-component\n\n# Time-delay embedding (Takens' theorem: dim=3, delay=5)\nembedding_dim = 3\ndelay = 5\nn_embedded = len(signal) - (embedding_dim - 1) * delay\nembedded = np.column_stack([signal[i * delay : i * delay + n_embedded] for i in range(embedding_dim)])\n\n# Euclidean distance matrix and binary threshold\ndistance_matrix = cdist(embedded, embedded, metric=\"euclidean\")\nthreshold = np.percentile(distance_matrix, 15)\nrecurrence_matrix = (distance_matrix <= threshold).astype(int)\n\n# Plot — square canvas for symmetric matrix (2400×2400 px)\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Binary colormap: PAGE_BG for non-recurrent, Imprint brand green for recurrent\ncmap = ListedColormap([PAGE_BG, BRAND])\nnorm = BoundaryNorm([0, 0.5, 1], cmap.N)\nax.imshow(recurrence_matrix, cmap=cmap, norm=norm, origin=\"lower\", interpolation=\"none\", aspect=\"equal\")\n\n# Axis labels and ticks\nax.set_xlabel(\"Time Index\", fontsize=10, color=INK, labelpad=8)\nax.set_ylabel(\"Time Index\", fontsize=10, color=INK, labelpad=8)\nax.xaxis.set_major_locator(ticker.MultipleLocator(100))\nax.yaxis.set_major_locator(ticker.MultipleLocator(100))\nax.xaxis.set_minor_locator(ticker.MultipleLocator(50))\nax.yaxis.set_minor_locator(ticker.MultipleLocator(50))\nax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, _: f\"{int(x)}\"))\nax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, _: f\"{int(x)}\"))\nax.tick_params(axis=\"both\", which=\"major\", labelsize=8, length=5, width=1.0, colors=INK_SOFT)\nax.tick_params(axis=\"both\", which=\"minor\", length=3, width=0.6, colors=INK_SOFT)\n\nfor spine in ax.spines.values():\n    spine.set_color(INK_SOFT)\n    spine.set_linewidth(0.8)\n\n# Regime annotations — elevated background boxes ensure text is always readable\nax.annotate(\n    \"periodic\\ntransient\",\n    xy=(60, 60),\n    xytext=(190, 185),\n    fontsize=8,\n    color=INK_SOFT,\n    fontweight=\"bold\",\n    ha=\"center\",\n    arrowprops={\"arrowstyle\": \"->\", \"color\": INK_MUTED, \"lw\": 1.2},\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.92, \"boxstyle\": \"round,pad=0.45\"},\n)\nax.annotate(\n    \"chaotic regime\",\n    xy=(290, 290),\n    xytext=(395, 175),\n    fontsize=8,\n    color=INK_SOFT,\n    fontweight=\"bold\",\n    ha=\"center\",\n    arrowprops={\"arrowstyle\": \"->\", \"color\": INK_MUTED, \"lw\": 1.2, \"connectionstyle\": \"arc3,rad=0.25\"},\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.92, \"boxstyle\": \"round,pad=0.45\"},\n)\n\nrr = np.sum(recurrence_matrix) / recurrence_matrix.size * 100\nax.text(\n    0.97,\n    0.02,\n    f\"RR = {rr:.1f}%\",\n    transform=ax.transAxes,\n    fontsize=8,\n    ha=\"right\",\n    va=\"bottom\",\n    bbox={\"boxstyle\": \"round,pad=0.4\", \"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.92},\n    color=BRAND,\n    fontweight=\"bold\",\n)\n\n# Titles — suptitle for mandatory format, subtitle for embedding context\nfig.suptitle(\"recurrence-basic · python · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, y=0.97)\nax.set_title(\n    \"Lorenz Attractor x-component  |  dim=3, τ=5, ε=percentile(15%)\",\n    fontsize=9,\n    color=INK_MUTED,\n    style=\"italic\",\n    pad=10,\n)\n\n# Save — figsize=(6,6) dpi=400 → exact 2400×2400 px; no bbox_inches='tight'\nplt.tight_layout(rect=[0.02, 0.02, 0.98, 0.92])\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}