{"spec_id":"line-timeseries-rolling","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nline-timeseries-rolling: Time Series with Rolling Average Overlay\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\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\n# Data - Daily temperature readings with 7-day rolling average\nnp.random.seed(42)\n\n# Generate 180 days of temperature data (6 months)\ndates = pd.date_range(\"2024-01-01\", periods=180, freq=\"D\")\n\n# Create seasonal temperature pattern with noise\n# Base seasonal pattern: winter -> spring -> summer\nday_of_year = np.arange(180)\nseasonal = 5 + 15 * np.sin(2 * np.pi * (day_of_year - 30) / 365)\nnoise = np.random.normal(0, 3, 180)\ntemperature = seasonal + noise\n\n# Create DataFrame and calculate rolling average\ndf = pd.DataFrame({\"date\": dates, \"temperature\": temperature})\ndf[\"rolling_avg\"] = df[\"temperature\"].rolling(window=7, center=True).mean()\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Raw data - thin, semi-transparent line (secondary Okabe-Ito color)\nax.plot(df[\"date\"], df[\"temperature\"], linewidth=1, alpha=0.4, color=\"#C475FD\", label=\"Daily Temperature\")\n\n# Rolling average - prominent smooth line (brand green)\nax.plot(df[\"date\"], df[\"rolling_avg\"], linewidth=3.5, color=\"#009E73\", label=\"7-Day Rolling Average\")\n\n# Labels and styling\nax.set_xlabel(\"Date\", fontsize=20, color=INK)\nax.set_ylabel(\"Temperature (°C)\", fontsize=20, color=INK)\nax.set_title(\"line-timeseries-rolling · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\n\n# Spine styling\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\n\n# Grid styling\nax.grid(True, alpha=0.1, linewidth=0.8, color=INK, axis=\"both\")\n\n# Legend styling\nleg = ax.legend(fontsize=16, loc=\"upper left\")\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\n# Format x-axis dates\nfig.autofmt_xdate(rotation=30)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}