{"spec_id":"line-timeseries-rolling","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nline-timeseries-rolling: Time Series with Rolling Average Overlay\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\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\nBRAND = \"#009E73\"  # Okabe-Ito position 1\nACCENT = \"#C475FD\"  # Okabe-Ito position 2\n\n# Data: Stock price with 20-day moving average (domain differs from temp/traffic)\nnp.random.seed(42)\n\n# Generate 252 trading days (1 year) of stock price data\ndates = pd.date_range(start=\"2024-01-01\", periods=252, freq=\"B\")\n\n# Stock price with uptrend, volatility, and correction pattern\nday_index = np.arange(252)\nbase_price = 100\n\n# Uptrend with noise and correction\ntrend = base_price + 0.05 * day_index + 3 * np.sin(2 * np.pi * day_index / 252)\nvolatility = np.random.normal(0, 2.5, 252)\nstock_price = trend + volatility\n\n# Create DataFrame\ndf = pd.DataFrame({\"date\": dates, \"price\": stock_price})\n\n# Calculate 20-day moving average (typical trading analysis window)\ndf[\"moving_avg\"] = df[\"price\"].rolling(window=20, center=False).mean()\n\n# Configure seaborn with theme-adaptive colors\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.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\nsns.set_context(\"talk\", font_scale=1.1)\n\n# Create figure\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\n\n# Plot raw data as semi-transparent line with slight prominence boost\nsns.lineplot(data=df, x=\"date\", y=\"price\", ax=ax, color=BRAND, alpha=0.5, linewidth=2, label=\"Daily Price\")\n\n# Plot moving average as prominent smooth line\nsns.lineplot(data=df, x=\"date\", y=\"moving_avg\", ax=ax, color=ACCENT, linewidth=4, label=\"20-Day Moving Average\")\n\n# Styling\nax.set_xlabel(\"Date\", fontsize=20, color=INK)\nax.set_ylabel(\"Stock Price ($)\", fontsize=20, color=INK)\nax.set_title(\"line-timeseries-rolling · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Rotate x-axis labels for readability\nplt.xticks(rotation=30, ha=\"right\")\n\n# Legend styling with reduced framealpha\nax.legend(fontsize=16, loc=\"upper left\", framealpha=0.85, edgecolor=INK_SOFT)\n\n# Remove top and right spines\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor spine in [\"left\", \"bottom\"]:\n    ax.spines[spine].set_color(INK_SOFT)\n\n# Grid on both axes for time series readability\nax.grid(True, alpha=0.15, linewidth=0.8, axis=\"both\")\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}