{"spec_id":"rug-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nrug-basic: Basic Rug Plot\nLibrary: bokeh 3.9.1 | Python 3.13.14\nQuality: 89/100 | Updated: 2026-07-25\n\"\"\"\n\nimport io\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\n\n# Prevent this file's directory from shadowing the installed bokeh package\nsys.path = [p for p in sys.path if os.path.abspath(p or os.getcwd()) != os.path.dirname(os.path.abspath(__file__))]\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool, Label, Range1d\nfrom bokeh.plotting import figure\nfrom PIL import Image\nfrom scipy import stats\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 — bimodal API response times (ms) showing clustering patterns\nnp.random.seed(42)\ncluster1 = np.random.normal(85, 12, 60)  # Fast responses (cache hits)\ncluster2 = np.random.normal(180, 20, 40)  # Slower responses (cache misses)\nvalues = np.concatenate([cluster1, cluster2])\n\n# KDE curve\nkde = stats.gaussian_kde(values, bw_method=0.3)\nx_smooth = np.linspace(values.min() - 20, values.max() + 20, 500)\nkde_y = kde(x_smooth)\n\n# Rug ticks sit just below y=0\nrug_top = 0.0\nrug_bottom = -kde_y.max() * 0.07\nrug_mid = (rug_top + rug_bottom) / 2\n\n# Sources\nkde_source = ColumnDataSource(data={\"x\": x_smooth, \"y\": kde_y})\nrug_source = ColumnDataSource(\n    data={\"x\": values, \"y0\": np.full(len(values), rug_bottom), \"y1\": np.full(len(values), rug_top)}\n)\nrug_hover_source = ColumnDataSource(data={\"x\": values, \"y_mid\": np.full(len(values), rug_mid)})\n\n# Figure — 3200×1800 landscape\np = figure(\n    width=3200,\n    height=1800,\n    title=\"rug-basic · python · bokeh · anyplot.ai\",\n    x_axis_label=\"Response Time (ms)\",\n    y_axis_label=\"Density\",\n    toolbar_location=None,\n    min_border_bottom=160,\n    min_border_left=180,\n    min_border_top=110,\n    min_border_right=50,\n)\n\n# KDE density — filled area plus edge line\np.varea(x=\"x\", y1=0, y2=\"y\", source=kde_source, fill_color=BRAND, fill_alpha=0.25)\np.line(\"x\", \"y\", source=kde_source, line_color=BRAND, line_width=4.5)\n\n# Rug ticks along the x-axis — thin line_width keeps individual ticks\n# distinguishable even where the fast-response cluster packs densely\np.segment(x0=\"x\", y0=\"y0\", x1=\"x\", y1=\"y1\", source=rug_source, line_color=BRAND, line_width=2.5, line_alpha=0.5)\n\n# Invisible scatter over the rug ticks — hit target for hover, showcasing\n# Bokeh's interactive strength (exact response time per observation)\nrug_hits = p.scatter(x=\"x\", y=\"y_mid\", source=rug_hover_source, size=24, fill_alpha=0, line_alpha=0)\nhover = HoverTool(renderers=[rug_hits], tooltips=[(\"Response Time\", \"@x{0.0} ms\")], mode=\"vline\")\np.add_tools(hover)\n\n# Axis ranges\np.x_range = Range1d(values.min() - 20, values.max() + 20)\np.y_range = Range1d(rug_bottom * 2.0, kde_y.max() * 1.35)\n\n# Cluster call-outs — guide the viewer through the bimodal story\nfor mode_x, mode_label in ((85, \"cache hits\"), (180, \"cache misses\")):\n    mode_y = float(kde(mode_x)[0])\n    p.add_layout(\n        Label(\n            x=mode_x,\n            y=mode_y + kde_y.max() * 0.08,\n            text=mode_label,\n            text_align=\"center\",\n            text_font_size=\"28pt\",\n            text_font_style=\"italic\",\n            text_color=INK_SOFT,\n        )\n    )\n\n# Text sizing — canonical bokeh values for 3200×1800\np.title.text_font_size = \"50pt\"\np.xaxis.axis_label_text_font_size = \"42pt\"\np.yaxis.axis_label_text_font_size = \"42pt\"\np.xaxis.major_label_text_font_size = \"34pt\"\np.yaxis.major_label_text_font_size = \"34pt\"\n\n# Theme-adaptive chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = None  # drop the frame for a cleaner, less \"default\" look\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 = None\np.ygrid.grid_line_color = INK\np.ygrid.grid_line_alpha = 0.10\n\n# Save HTML (interactive catalog artifact)\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome (Selenium 4 / Selenium Manager).\n# Chrome's internal UI overhead shrinks the viewport below --window-size by ~139 px.\n# Use a taller window (H + 200 buffer) so the viewport is >= H, then crop to exact dims.\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 + 200}\",\n    \"--hide-scrollbars\",\n    \"--force-device-scale-factor=1\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H + 200)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\nraw = driver.get_screenshot_as_png()\ndriver.quit()\nimg = Image.open(io.BytesIO(raw)).crop((0, 0, W, H))\nimg.save(f\"plot-{THEME}.png\")\n"}