{"spec_id":"span-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nspan-basic: Basic Span Plot (Highlighted Region)\nLibrary: bokeh 3.9.1 | Python 3.13.14\nQuality: 94/100 | Updated: 2026-07-25\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import BoxAnnotation, ColumnDataSource, HoverTool, Label\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\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — always first series\n\n# Data - Monthly revenue over 2 years with spans highlighting key periods\nnp.random.seed(42)\nmonths = np.arange(1, 25)\nbase_revenue = 100 + np.linspace(0, 50, 24) + 15 * np.sin(np.linspace(0, 4 * np.pi, 24))\nnoise = np.random.randn(24) * 8\nrevenue = base_revenue + noise\n\n# Create ColumnDataSource\nsource = ColumnDataSource(data={\"x\": months, \"y\": revenue})\n\n# Create figure (3200 × 1800 px — Step 0 canonical canvas)\np = figure(\n    width=3200,\n    height=1800,\n    title=\"span-basic · python · bokeh · anyplot.ai\",\n    x_axis_label=\"Month\",\n    y_axis_label=\"Revenue (thousands $)\",\n    toolbar_location=None,  # bokeh's default toolbar adds ~30-50px, shrinking the saved PNG\n    min_border_bottom=160,\n    min_border_left=180,\n    min_border_top=110,\n    min_border_right=50,\n)\n\n# Add vertical span - highlight Q4 of Year 1 (months 10-12)\nvertical_span = BoxAnnotation(\n    left=10, right=12, fill_alpha=0.25, fill_color=\"#4467A3\", line_color=\"#4467A3\", line_width=2, line_alpha=0.5\n)\np.add_layout(vertical_span)\n\n# Add horizontal span - highlight target revenue range (122-135) using Imprint amber\nhorizontal_span = BoxAnnotation(\n    bottom=122, top=135, fill_alpha=0.22, fill_color=\"#DDCC77\", line_color=\"#DDCC77\", line_width=2, line_alpha=0.5\n)\np.add_layout(horizontal_span)\n\n# Plot line with markers (Imprint palette position 1)\np.line(x=\"x\", y=\"y\", source=source, line_width=4, line_color=BRAND)\np.scatter(x=\"x\", y=\"y\", source=source, size=16, fill_color=BRAND, line_color=PAGE_BG, line_width=2)\n\n# HoverTool — showcases Bokeh's interactive HTML output\nhover = HoverTool(tooltips=[(\"Month\", \"@x\"), (\"Revenue\", \"@y{0.1} K$\")])\np.add_tools(hover)\n\n# Add labels for spans — INK keeps full contrast against either theme,\n# independent of the span's own tint (unlike the span-colored text used before)\nvertical_label = Label(\n    x=10.2, y=140, text=\"Q4 Peak Season\", text_font_size=\"28pt\", text_color=INK, text_font_style=\"bold\"\n)\np.add_layout(vertical_label)\n\nhorizontal_label = Label(\n    x=6.5, y=127, text=\"Target Range\", text_font_size=\"28pt\", text_color=INK, text_font_style=\"bold\"\n)\np.add_layout(horizontal_label)\n\n# Apply theme-adaptive chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = None  # remove box border; L-shaped spines via xaxis/yaxis lines only\n\np.title.text_color = INK\np.title.text_font_size = \"50pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.axis_label_text_font_size = \"42pt\"\np.yaxis.axis_label_text_font_size = \"42pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\np.xaxis.major_label_text_font_size = \"34pt\"\np.yaxis.major_label_text_font_size = \"34pt\"\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\np.xgrid.grid_line_color = None  # y-only grid preferred for line charts\np.ygrid.grid_line_color = INK\np.ygrid.grid_line_alpha = 0.10\n\n# Save — write the interactive HTML, then screenshot it with headless Chrome\n# (bokeh.io.export_png is unreliable in this environment's chromedriver setup)\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\nW, H = 3200, 1800\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()}\")\ndriver.execute_cdp_cmd(\n    \"Emulation.setDeviceMetricsOverride\", {\"width\": W, \"height\": H, \"deviceScaleFactor\": 1, \"mobile\": False}\n)\ntime.sleep(3)  # let bokeh's JS render the canvas\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}