{"spec_id":"line-confidence","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nline-confidence: Line Plot with Confidence Interval\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-09\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 ColumnDataSource, HoverTool, Legend\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# Theme-adaptive colors\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 - Simulating model predictions with 95% confidence interval\nnp.random.seed(42)\nx = np.linspace(0, 10, 50)\n\n# True underlying function with some curvature\ny_true = 2 + 0.5 * x + 0.1 * x**2\n\n# Add noise to create observed \"predictions\"\ny = y_true + np.random.normal(0, 0.5, len(x))\n\n# Confidence interval (widens slightly over prediction horizon)\nuncertainty = 0.8 + 0.15 * x\ny_lower = y - 1.96 * uncertainty\ny_upper = y + 1.96 * uncertainty\n\n# Create ColumnDataSource\nsource = ColumnDataSource(data={\"x\": x, \"y\": y, \"y_lower\": y_lower, \"y_upper\": y_upper})\n\n# Create figure (4800 x 2700 px for 16:9 aspect ratio)\np = figure(\n    width=4800,\n    height=2700,\n    title=\"line-confidence · bokeh · pyplots.ai\",\n    x_axis_label=\"Time (units)\",\n    y_axis_label=\"Predicted Value\",\n)\n\n# Style the plot - scaled for large canvas\np.title.text_font_size = \"28pt\"\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\n\n# Add confidence band using varea\nband = p.varea(x=\"x\", y1=\"y_lower\", y2=\"y_upper\", source=source, fill_color=\"#009E73\", fill_alpha=0.3)\n\n# Add central trend line\nline = p.line(x=\"x\", y=\"y\", source=source, line_color=\"#009E73\", line_width=4)\n\n# Add HoverTool for interactivity\nhover = HoverTool(tooltips=[(\"Time\", \"@x{0.00}\"), (\"Prediction\", \"@y{0.00}\")])\np.add_tools(hover)\n\n# Add legend with improved placement\nlegend = Legend(items=[(\"Prediction\", [line]), (\"95% Confidence Interval\", [band])], location=\"bottom_right\")\nlegend.label_text_font_size = \"18pt\"\nlegend.glyph_height = 30\nlegend.glyph_width = 30\nlegend.spacing = 15\nlegend.padding = 20\nlegend.background_fill_alpha = 0.9\np.add_layout(legend)\n\n# Theme-adaptive styling\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\np.title.text_color = INK\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\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 = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\n\nif p.legend:\n    p.legend.background_fill_color = ELEVATED_BG\n    p.legend.border_line_color = INK_SOFT\n    p.legend.label_text_color = INK_SOFT\n\n# Write the interactive HTML (also a required catalog artifact)\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot it with headless Chrome — Selenium 4 / Selenium Manager\n# auto-resolves a working driver for the system Chrome.\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)  # let bokeh's JS render the canvas\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}