{"spec_id":"density-rug","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\ndensity-rug: Density Plot with Rug Marks\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 95/100 | Updated: 2026-05-18\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.models import ColumnDataSource\nfrom bokeh.plotting import figure, output_file, save\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# Theme tokens (see prompts/default-style-guide.md)\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\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1 — ALWAYS first series\n\n# Data - Response times in milliseconds (realistic API latency data)\nnp.random.seed(42)\n# Mix of normal operations and some slower responses (bimodal-ish)\nnormal_times = np.random.normal(loc=120, scale=25, size=180)\nslow_times = np.random.normal(loc=220, scale=30, size=40)\nresponse_times = np.concatenate([normal_times, slow_times])\nresponse_times = response_times[response_times > 0]  # Ensure positive values\n\n# Compute KDE (Gaussian kernel density estimation)\nn = len(response_times)\nstd = np.std(response_times)\niqr = np.percentile(response_times, 75) - np.percentile(response_times, 25)\nbandwidth = 0.9 * min(std, iqr / 1.34) * n ** (-0.2)  # Silverman's rule\n\nx_range = np.linspace(response_times.min() - 20, response_times.max() + 20, 500)\ndensity = np.zeros_like(x_range)\nfor xi in response_times:\n    density += np.exp(-0.5 * ((x_range - xi) / bandwidth) ** 2)\ndensity = density / (n * bandwidth * np.sqrt(2 * np.pi))\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"density-rug · Python · bokeh · anyplot.ai\",\n    x_axis_label=\"Response Time (ms)\",\n    y_axis_label=\"Density\",\n)\n\n# Plot KDE as filled area using patch\nkde_x = np.concatenate([[x_range[0]], x_range, [x_range[-1]]])\nkde_y = np.concatenate([[0], density, [0]])\np.patch(x=kde_x, y=kde_y, fill_color=BRAND, fill_alpha=0.35, line_color=BRAND, line_width=4)\n\n# Plot KDE line on top\nkde_source = ColumnDataSource(data={\"x\": x_range, \"y\": density})\np.line(x=\"x\", y=\"y\", source=kde_source, line_color=BRAND, line_width=5)\n\n# Rug marks along x-axis\nrug_height = density.max() * 0.03  # Small tick height relative to density\nrug_source = ColumnDataSource(\n    data={\"x\": response_times, \"y0\": np.zeros(len(response_times)), \"y1\": np.full(len(response_times), -rug_height)}\n)\np.segment(x0=\"x\", y0=\"y0\", x1=\"x\", y1=\"y1\", source=rug_source, line_color=BRAND, line_width=2, line_alpha=0.5)\n\n# Styling - sizes for 4800x2700 canvas\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\n\n# Grid - subtle, not prominent\np.xgrid.grid_line_alpha = 0.15\np.ygrid.grid_line_alpha = 0.15\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\n\n# Theme-adaptive chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_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\n# Extend y-axis slightly below 0 to show rug marks\np.y_range.start = -rug_height * 1.5\np.y_range.end = density.max() * 1.1\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot it with headless Chrome — Selenium 4 / Selenium Manager\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"}