{"spec_id":"scatter-lag","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nscatter-lag: Lag Plot for Time Series Autocorrelation Diagnosis\nLibrary: matplotlib 3.11.0 | Python 3.13.14\nQuality: 90/100 | Updated: 2026-06-24\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove script directory from path so \"import matplotlib\" finds the package, not this file\nsys.path.pop(0)\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import LinearSegmentedColormap\n\n\n# Theme 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\"\n\n# Imprint sequential colormap for time index (single-polarity continuous)\nimprint_seq = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#4467A3\"])\n\n# Data — synthetic AR(1) process with strong positive autocorrelation\nnp.random.seed(42)\nn_observations = 500\nphi = 0.85\nnoise = np.random.normal(0, 1, n_observations)\nseries = np.zeros(n_observations)\nseries[0] = noise[0]\nfor i in range(1, n_observations):\n    series[i] = phi * series[i - 1] + noise[i]\n\nlag = 1\ny_t = series[:-lag]\ny_t_lag = series[lag:]\ntime_index = np.arange(len(y_t))\n\nr_value = np.corrcoef(y_t, y_t_lag)[0, 1]\n\n# Plot\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nscatter = ax.scatter(\n    y_t, y_t_lag, c=time_index, cmap=imprint_seq, s=55, alpha=0.50, edgecolors=PAGE_BG, linewidth=0.3, zorder=2\n)\n\n# Diagonal reference line (y = x)\ndata_min = min(y_t.min(), y_t_lag.min())\ndata_max = max(y_t.max(), y_t_lag.max())\nmargin = (data_max - data_min) * 0.05\nax.plot(\n    [data_min - margin, data_max + margin],\n    [data_min - margin, data_max + margin],\n    color=INK_SOFT,\n    linewidth=1.5,\n    linestyle=\"--\",\n    alpha=0.5,\n    zorder=1,\n)\n\n# Colorbar\ncbar = fig.colorbar(scatter, ax=ax, pad=0.03, aspect=28)\ncbar.set_label(\"Time Index\", fontsize=10, color=INK_SOFT)\ncbar.ax.tick_params(labelsize=8, labelcolor=INK_SOFT, color=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\ncbar.outline.set_linewidth(0.5)\n\n# Correlation annotation with qualitative descriptor for richer storytelling\ndirection = \"positive\" if r_value > 0 else \"negative\"\nstrength = \"Strong\" if abs(r_value) >= 0.7 else (\"Moderate\" if abs(r_value) >= 0.4 else \"Weak\")\nax.text(\n    0.04,\n    0.96,\n    f\"r = {r_value:.3f}  ·  {strength} {direction} autocorrelation\",\n    transform=ax.transAxes,\n    fontsize=9,\n    verticalalignment=\"top\",\n    fontweight=\"medium\",\n    color=INK,\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": \"none\", \"alpha\": 0.85, \"pad\": 4},\n)\n\n# Style\nax.set_xlabel(\"y(t)\", fontsize=10, color=INK)\nax.set_ylabel(f\"y(t + {lag})\", fontsize=10, color=INK)\n\ntitle = \"scatter-lag · python · matplotlib · anyplot.ai\"\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK)\n\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\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\n# Both-axis grid for scatter plots (style guide recommendation)\nax.xaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\nax.set_axisbelow(True)\n\nfig.subplots_adjust(left=0.09, right=0.86, top=0.93, bottom=0.10)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}