{"spec_id":"line-timeseries-rolling","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nline-timeseries-rolling: Time Series with Rolling Average Overlay\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-13\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nimport pandas as pd\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Data - Simulated daily stock prices with 20-day rolling average\nnp.random.seed(42)\nn_days = 250\ndates = pd.date_range(\"2024-01-01\", periods=n_days, freq=\"D\")\n\n# Create realistic stock price data with trend and volatility\ntrend = np.linspace(100, 120, n_days)\nnoise = np.random.normal(0, 3, n_days)\nprices = trend + noise\n\n# Calculate 20-day rolling average\nrolling_window = 20\nrolling_avg = pd.Series(prices).rolling(window=rolling_window, center=True).mean()\n\n# Create DataFrame\ndf = pd.DataFrame({\"date\": dates, \"price\": prices, \"rolling_avg\": rolling_avg})\n\n# Create ColumnDataSource for raw data\nsource_raw = ColumnDataSource(data={\"date\": df[\"date\"], \"price\": df[\"price\"]})\n\n# Create ColumnDataSource for rolling average (exclude NaN values)\ndf_rolling = df.dropna(subset=[\"rolling_avg\"])\nsource_rolling = ColumnDataSource(data={\"date\": df_rolling[\"date\"], \"rolling_avg\": df_rolling[\"rolling_avg\"]})\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"line-timeseries-rolling · bokeh · anyplot.ai\",\n    x_axis_label=\"Date\",\n    y_axis_label=\"Stock Price ($)\",\n    x_axis_type=\"datetime\",\n    tools=\"pan,wheel_zoom,box_zoom,reset,save\",\n)\n\n# Plot raw data - thin line with transparency\np.line(\n    x=\"date\",\n    y=\"price\",\n    source=source_raw,\n    line_width=2,\n    line_alpha=0.4,\n    line_color=IMPRINT[0],\n    legend_label=\"Raw Data\",\n)\n\n# Plot rolling average - prominent smooth line\np.line(\n    x=\"date\",\n    y=\"rolling_avg\",\n    source=source_rolling,\n    line_width=4,\n    line_color=IMPRINT[1],\n    legend_label=f\"{rolling_window}-Day Rolling Average\",\n)\n\n# Styling for large canvas\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\n\np.xaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_color = INK\n\np.xaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_color = INK_SOFT\n\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\n# Grid styling - subtle\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.15\np.ygrid.grid_line_alpha = 0.15\n\n# Background\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\n# Legend styling\np.legend.label_text_font_size = \"18pt\"\np.legend.label_text_color = INK_SOFT\np.legend.location = \"top_left\"\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\n\n# Save as HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome — Selenium 4 / Selenium Manager auto-resolves\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}