{"spec_id":"scatter-lag","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nscatter-lag: Lag Plot for Time Series Autocorrelation Diagnosis\nLibrary: letsplot 4.10.1 | Python 3.13.14\nQuality: 89/100 | Updated: 2026-06-24\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\n\n\nLetsPlot.setup_html()\n\n# Theme-adaptive chrome — Imprint palette\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\"\nGRID = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint sequential colormap: brand-green → blue (single-polarity continuous)\nSEQ_LOW = \"#009E73\"  # position 1\nSEQ_HIGH = \"#4467A3\"  # position 3\n\n# Data: AR(1) temperature process, phi=0.85 → strong positive autocorrelation\nnp.random.seed(42)\nn = 400\nlag = 1\nphi = 0.85\ninnovations = np.random.randn(n) * 2.0\n\ntemperature = np.zeros(n)\ntemperature[0] = 20.0\nfor i in range(1, n):\n    temperature[i] = phi * temperature[i - 1] + (1 - phi) * 20.0 + innovations[i]\n\n# Lag plot data: y(t) vs y(t+lag)\nvalue_t = temperature[:-lag]\nvalue_t_lag = temperature[lag:]\ntime_index = np.arange(len(value_t))\ndf = pd.DataFrame({\"value_t\": value_t, \"value_t_lag\": value_t_lag, \"day\": time_index})\n\n# Autocorrelation at lag 1\nr = np.corrcoef(value_t, value_t_lag)[0, 1]\n\n# Diagonal reference line (y = x)\nref_min = min(value_t.min(), value_t_lag.min()) - 1\nref_max = max(value_t.max(), value_t_lag.max()) + 1\nref_df = pd.DataFrame({\"x\": [ref_min, ref_max], \"y\": [ref_min, ref_max]})\n\n# Correlation annotation: placed at bottom-right where point density is low\nanno_df = pd.DataFrame({\"x\": [ref_max - 1.5], \"y\": [ref_min + 1.5], \"label\": [f\"r = {r:.2f}\"]})\n\nplot = (\n    ggplot(df, aes(x=\"value_t\", y=\"value_t_lag\", color=\"day\"))\n    # Reference diagonal: y = x (perfect lag-1 autocorrelation)\n    + geom_line(\n        aes(x=\"x\", y=\"y\"),\n        data=ref_df,\n        color=INK_SOFT,\n        size=0.8,\n        linetype=\"dashed\",\n        inherit_aes=False,\n    )\n    # OLS regression line — letsplot-native geom_smooth with confidence band\n    + geom_smooth(\n        aes(x=\"value_t\", y=\"value_t_lag\"),\n        data=df,\n        method=\"lm\",\n        color=\"#C475FD\",\n        fill=\"#C475FD\",\n        size=1.2,\n        alpha=0.12,\n        inherit_aes=False,\n    )\n    # Data points colored by temporal order (Imprint sequential: green → blue)\n    + geom_point(\n        size=2.5,\n        alpha=0.45,\n        shape=16,\n        tooltips=layer_tooltips()\n        .line(\"Day|@day\")\n        .line(\"y(t)|@{value_t}{.2f}\")\n        .line(\"y(t+1)|@{value_t_lag}{.2f}\"),\n    )\n    # Correlation coefficient annotation\n    + geom_text(\n        aes(x=\"x\", y=\"y\", label=\"label\"),\n        data=anno_df,\n        size=5,\n        color=INK,\n        family=\"monospace\",\n        hjust=1.0,\n        inherit_aes=False,\n    )\n    + scale_color_gradient(\n        low=SEQ_LOW, high=SEQ_HIGH, name=\"Day\"\n    )\n    + labs(\n        x=\"y(t)\",\n        y=f\"y(t + {lag})\",\n        title=\"scatter-lag · python · letsplot · anyplot.ai\",\n        caption=\"AR(1) simulated daily temperature · dashed = y = x, purple = OLS fit ± 95% CI\",\n    )\n    + ggsize(800, 450)\n    + theme_minimal()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=GRID, size=0.3),\n        panel_grid_minor=element_blank(),\n        axis_text=element_text(size=10, color=INK_SOFT),\n        axis_title=element_text(size=12, color=INK),\n        axis_line=element_line(color=INK_SOFT),\n        axis_ticks=element_line(color=INK_SOFT, size=0.3),\n        plot_title=element_text(size=16, color=INK, face=\"bold\"),\n        plot_caption=element_text(size=8, color=INK_MUTED, face=\"italic\"),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n        legend_title=element_text(size=10, color=INK),\n        legend_text=element_text(size=10, color=INK_SOFT),\n        plot_margin=[30, 40, 20, 20],\n    )\n)\n\n# Save PNG (3200×1800) and interactive HTML\nggsave(plot, f\"plot-{THEME}.png\", path=\".\", scale=4)\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}