{"spec_id":"line-timeseries-rolling","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nline-timeseries-rolling: Time Series with Rolling Average Overlay\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nimport plotly.graph_objects as go\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\"\nGRID = \"rgba(26,26,23,0.25)\" if THEME == \"light\" else \"rgba(240,239,232,0.25)\"\n\n# Okabe-Ito palette\nBRAND = \"#009E73\"  # Raw data (first series)\nACCENT = \"#C475FD\"  # Rolling average\n\n# Data - Daily temperature readings with noise\nnp.random.seed(42)\n\n# Generate 200 days of data\ndates = pd.date_range(\"2024-01-01\", periods=200, freq=\"D\")\n\n# Create realistic temperature pattern with seasonal trend + noise\ndays = np.arange(200)\nseasonal = 15 * np.sin(2 * np.pi * days / 365 - np.pi / 2)\ntrend = 0.02 * days\nnoise = np.random.randn(200) * 3\nbase_temp = 12\n\nraw_values = base_temp + seasonal + trend + noise\n\n# Create DataFrame\ndf = pd.DataFrame({\"date\": dates, \"value\": raw_values})\n\n# Calculate 14-day rolling average\nwindow_size = 14\ndf[\"rolling_avg\"] = df[\"value\"].rolling(window=window_size, center=False).mean()\n\n# Create figure\nfig = go.Figure()\n\n# Raw data - lighter, semi-transparent line\nfig.add_trace(\n    go.Scatter(x=df[\"date\"], y=df[\"value\"], mode=\"lines\", name=\"Raw Data\", line=dict(color=BRAND, width=2), opacity=0.4)\n)\n\n# Rolling average - prominent smooth line\nfig.add_trace(\n    go.Scatter(\n        x=df[\"date\"],\n        y=df[\"rolling_avg\"],\n        mode=\"lines\",\n        name=f\"{window_size}-Day Rolling Average\",\n        line=dict(color=ACCENT, width=4),\n    )\n)\n\n# Layout for 4800x2700 canvas\nfig.update_layout(\n    title=dict(\n        text=\"line-timeseries-rolling · plotly · anyplot.ai\", font=dict(size=28, color=INK), x=0.5, xanchor=\"center\"\n    ),\n    xaxis=dict(\n        title=dict(text=\"Date\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        showgrid=True,\n        gridwidth=1,\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Temperature (°C)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        showgrid=True,\n        gridwidth=1,\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        zerolinecolor=INK_SOFT,\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    legend=dict(\n        font=dict(size=20, color=INK_SOFT),\n        x=0.02,\n        y=0.98,\n        xanchor=\"left\",\n        yanchor=\"top\",\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n    ),\n    margin=dict(l=120, r=60, t=100, b=100),\n)\n\n# Save as PNG (4800x2700) and HTML\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}