{"spec_id":"scatter-lag","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nscatter-lag: Lag Plot for Time Series Autocorrelation Diagnosis\nLibrary: seaborn 0.13.2 | Python 3.13.14\nQuality: 87/100 | Updated: 2026-06-24\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib.colors import LinearSegmentedColormap\n\n\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\n\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 cmap for temporal index (single-polarity continuous)\nimprint_seq = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#4467A3\"])\n\n# Data — AR(1) with extreme positive autocorrelation (phi=0.9, n=1500)\nnp.random.seed(42)\nn = 1500\nphi = 0.9\nnoise = np.random.normal(0, 1, n)\nvalues = np.zeros(n)\nvalues[0] = noise[0]\nfor t in range(1, n):\n    values[t] = phi * values[t - 1] + noise[t]\n\nlag = 1\ny_t = values[:-lag]\ny_t_lag = values[lag:]\ntime_index = np.arange(len(y_t))\n\ndf = pd.DataFrame({\"y(t)\": y_t, \"y(t+1)\": y_t_lag, \"Time Index\": time_index})\nr = np.corrcoef(y_t, y_t_lag)[0, 1]\n\n# Theme-adaptive chrome\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400)\nfig.patch.set_facecolor(PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Density contours via sns.kdeplot — visual depth separating dense cluster from sparse outer points\nsns.kdeplot(data=df, x=\"y(t)\", y=\"y(t+1)\", levels=4, color=INK_SOFT, linewidths=0.8, alpha=0.45, ax=ax, zorder=1)\n\n# Diagonal reference line (y = x) using theme-adaptive neutral\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.04\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.65,\n    zorder=2,\n)\n\n# Temporal scatter via sns.scatterplot with continuous hue — seaborn-idiomatic coloring\nnorm = plt.Normalize(df[\"Time Index\"].min(), df[\"Time Index\"].max())\nsm = plt.cm.ScalarMappable(cmap=imprint_seq, norm=norm)\nsm.set_array([])\n\nsns.scatterplot(\n    data=df,\n    x=\"y(t)\",\n    y=\"y(t+1)\",\n    hue=\"Time Index\",\n    palette=imprint_seq,\n    hue_norm=norm,\n    s=8,\n    alpha=0.35,\n    edgecolor=\"none\",\n    legend=False,\n    ax=ax,\n    zorder=3,\n)\n\n# Colorbar for temporal structure\ncbar = plt.colorbar(sm, ax=ax, pad=0.02, aspect=28)\ncbar.set_label(\"Time Index\", fontsize=9, color=INK_SOFT)\ncbar.ax.tick_params(labelsize=8, colors=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\ncbar.outline.set_linewidth(0.5)\n\n# Correlation coefficient annotation\nax.annotate(\n    f\"r = {r:.2f}\",\n    xy=(0.04, 0.95),\n    xycoords=\"axes fraction\",\n    fontsize=9,\n    fontweight=\"bold\",\n    color=INK,\n    ha=\"left\",\n    va=\"top\",\n    bbox={\"boxstyle\": \"round,pad=0.4\", \"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.9, \"linewidth\": 0.8},\n)\n\nax.set_title(\"scatter-lag · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, pad=12)\nax.set_xlabel(\"y(t)\", fontsize=10, color=INK)\nax.set_ylabel(\"y(t+1)\", fontsize=10, color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\nax.grid(True, alpha=0.15, linewidth=0.6, color=INK)\nsns.despine(ax=ax)\n\nfig.subplots_adjust(left=0.09, right=0.91, top=0.93, bottom=0.12)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}