{"spec_id":"scatter-lag","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nscatter-lag: Lag Plot for Time Series Autocorrelation Diagnosis\nLibrary: plotly 6.8.0 | Python 3.13.14\nQuality: 89/100 | Updated: 2026-06-24\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove the script's own directory from sys.path to prevent shadowing the installed\n# plotly package (Python inserts the script dir as sys.path[0] when running a .py file).\n_script_dir = os.path.dirname(os.path.abspath(__file__))\nif sys.path and sys.path[0] == _script_dir:\n    sys.path.pop(0)\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme-adaptive chrome — Imprint palette 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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nRULE = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint sequential colormap (brand green → blue) for continuous time index\nimprint_seq = [[0.0, \"#009E73\"], [1.0, \"#4467A3\"]]\n\n# Data — synthetic AR(1) temperature process with strong autocorrelation\nnp.random.seed(42)\nn_points = 500\nphi = 0.85\nnoise = np.random.normal(0, 1, n_points)\ntemperature = np.zeros(n_points)\ntemperature[0] = 20.0\nfor i in range(1, n_points):\n    temperature[i] = phi * temperature[i - 1] + (1 - phi) * 20.0 + noise[i]\n\nlag = 1\ny_t = temperature[:-lag]\ny_t_lag = temperature[lag:]\ntime_index = np.arange(len(y_t))\n\n# Correlation coefficient\ncorrelation = np.corrcoef(y_t, y_t_lag)[0, 1]\n\n# Regression line through the data\nslope, intercept = np.polyfit(y_t, y_t_lag, 1)\nx_fit = np.array([y_t.min(), y_t.max()])\ny_fit = slope * x_fit + intercept\n\n# Diagonal reference line bounds\ndata_min = min(y_t.min(), y_t_lag.min())\ndata_max = max(y_t.max(), y_t_lag.max())\npadding = (data_max - data_min) * 0.05\nline_min = data_min - padding\nline_max = data_max + padding\n\n# Plot\nfig = go.Figure()\n\n# Scatter points colored by time index using Imprint sequential colormap\nfig.add_trace(\n    go.Scatter(\n        x=y_t,\n        y=y_t_lag,\n        mode=\"markers\",\n        marker={\n            \"size\": 9,\n            \"color\": time_index,\n            \"colorscale\": imprint_seq,\n            \"colorbar\": {\n                \"title\": {\"text\": \"Time Index\", \"font\": {\"size\": 12, \"color\": INK}},\n                \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n                \"thickness\": 16,\n                \"len\": 0.65,\n                \"outlinewidth\": 0,\n                \"y\": 0.5,\n                \"bgcolor\": PAGE_BG,\n                \"tickcolor\": INK_SOFT,\n            },\n            \"opacity\": 0.6,\n            \"line\": {\"width\": 0.3, \"color\": \"rgba(255,255,255,0.4)\"},\n        },\n        hovertemplate=(\n            \"<b>Time %{customdata}</b><br>Temp at t: %{x:.1f} °C<br>Temp at t+1: %{y:.1f} °C<extra></extra>\"\n        ),\n        customdata=time_index,\n    )\n)\n\n# Diagonal reference line (y = x)\nfig.add_trace(\n    go.Scatter(\n        x=[line_min, line_max],\n        y=[line_min, line_max],\n        mode=\"lines\",\n        line={\"color\": INK_MUTED, \"width\": 1.5, \"dash\": \"dot\"},\n        showlegend=False,\n        hoverinfo=\"skip\",\n        name=\"y = x\",\n    )\n)\n\n# Regression trend line — Imprint blue (position 3)\nfig.add_trace(\n    go.Scatter(\n        x=x_fit,\n        y=y_fit,\n        mode=\"lines\",\n        line={\"color\": \"#4467A3\", \"width\": 2.5},\n        showlegend=False,\n        hoverinfo=\"skip\",\n        name=\"trend\",\n    )\n)\n\nfig.update_layout(\n    autosize=False,\n    title={\n        \"text\": \"scatter-lag · python · plotly · anyplot.ai\",\n        \"font\": {\"size\": 16, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n        \"y\": 0.97,\n    },\n    xaxis={\n        \"title\": {\"text\": \"Temperature (°C) at time t\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"showgrid\": True,\n        \"gridcolor\": RULE,\n        \"gridwidth\": 1,\n        \"zeroline\": False,\n        \"showline\": False,\n        \"ticks\": \"\",\n        \"linecolor\": INK_SOFT,\n    },\n    yaxis={\n        \"title\": {\"text\": f\"Temperature (°C) at time t+{lag}\", \"font\": {\"size\": 12, \"color\": INK}},\n        \"tickfont\": {\"size\": 10, \"color\": INK_SOFT},\n        \"showgrid\": True,\n        \"gridcolor\": RULE,\n        \"gridwidth\": 1,\n        \"zeroline\": False,\n        \"showline\": False,\n        \"ticks\": \"\",\n        \"linecolor\": INK_SOFT,\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font={\"color\": INK},\n    showlegend=False,\n    margin={\"l\": 110, \"r\": 150, \"t\": 120, \"b\": 110},\n)\n\n# Subtitle annotation\nfig.add_annotation(\n    text=\"AR(1) process  |  lag = 1  |  500 observations\",\n    xref=\"paper\",\n    yref=\"paper\",\n    x=0.5,\n    y=1.07,\n    showarrow=False,\n    font={\"size\": 10, \"color\": INK_MUTED},\n    xanchor=\"center\",\n)\n\n# Correlation coefficient annotation\nfig.add_annotation(\n    text=f\"<b>r = {correlation:.3f}</b>\",\n    xref=\"paper\",\n    yref=\"paper\",\n    x=0.04,\n    y=0.97,\n    showarrow=False,\n    font={\"size\": 12, \"color\": INK},\n    bgcolor=ELEVATED_BG,\n    bordercolor=INK_SOFT,\n    borderwidth=1,\n    borderpad=8,\n)\n\n# Save — landscape 3200×1800 (width=800 height=450 scale=4)\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}