{"spec_id":"ecdf-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\necdf-basic: Basic ECDF Plot\nLibrary: bokeh 3.9.1 | Python 3.13.14\nQuality: 89/100 | Updated: 2026-06-25\n\"\"\"\n\nimport io\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\n\n# Remove script directory from sys.path to avoid shadowing the bokeh package\nsys.path = [p for p in sys.path if Path(p).resolve() != Path(__file__).resolve().parent]\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import BoxAnnotation, ColumnDataSource, CrosshairTool, CustomJSHover, HoverTool, Label, Span\nfrom bokeh.plotting import figure\nfrom PIL import Image\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\"\nBRAND = \"#009E73\"  # Imprint palette position 1 — always first series\n\n# Data: marathon finish times (minutes) for 300 recreational runners\nnp.random.seed(42)\nn_runners = 300\nfinish_times = np.random.normal(loc=240, scale=32, size=n_runners)\n\n# ECDF calculation\nsorted_times = np.sort(finish_times)\ncumulative = np.arange(1, n_runners + 1) / n_runners\n\n# Key percentiles\nq25, q50, q75 = np.percentile(sorted_times, [25, 50, 75])\n\n# Staircase x/y for area fill matching step-after mode exactly\nstep_x_fill = np.concatenate([[sorted_times[0]], np.repeat(sorted_times[1:], 2)])\nstep_y_fill = np.repeat(cumulative, 2)[:-1]\n\nsource = ColumnDataSource(data={\"x\": sorted_times, \"y\": cumulative})\n\n# Plot — 3200×1800 landscape canvas (hard rule)\ntitle = \"Marathon Finish Times · ecdf-basic · python · bokeh · anyplot.ai\"\np = figure(\n    width=3200,\n    height=1800,\n    title=title,\n    x_axis_label=\"Finish Time (minutes)\",\n    y_axis_label=\"Cumulative Proportion of Runners\",\n    y_range=(0, 1.05),\n    background_fill_color=PAGE_BG,\n    border_fill_color=PAGE_BG,\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# IQR shaded band (Q1–Q3): focal emphasis for distribution spread\niqr_band = BoxAnnotation(left=q25, right=q75, fill_color=BRAND, fill_alpha=0.08, line_color=None)\np.add_layout(iqr_band)\n\n# Subtle area fill under the ECDF step curve\np.varea(x=step_x_fill, y1=np.zeros_like(step_x_fill), y2=step_y_fill, fill_color=BRAND, fill_alpha=0.07)\n\n# ECDF step line — step-after matches the 1/n jump at each observation\nstep_renderer = p.step(x=\"x\", y=\"y\", source=source, line_width=4.5, line_color=BRAND, mode=\"after\")\n\n# Horizontal reference at y=0.5 — makes median reading effortless\np.add_layout(\n    Span(location=0.5, dimension=\"width\", line_color=INK_SOFT, line_dash=\"dotted\", line_width=2, line_alpha=0.45)\n)\n\n# Percentile vertical reference lines with staggered labels to avoid overlap\npercentile_annotations = [(q25, \"25th\", 0.03), (q50, \"50th (median)\", 0.09), (q75, \"75th\", 0.15)]\nfor q_val, q_lbl, y_pos in percentile_annotations:\n    p.add_layout(\n        Span(location=q_val, dimension=\"height\", line_color=INK_SOFT, line_dash=\"dashed\", line_width=2, line_alpha=0.55)\n    )\n    p.add_layout(\n        Label(\n            x=q_val,\n            y=y_pos,\n            text=f\"{q_lbl}: {q_val:.0f} min\",\n            text_font_size=\"30pt\",\n            text_color=INK,\n            text_font_style=\"italic\",\n            x_offset=14,\n            background_fill_color=ELEVATED_BG,\n            background_fill_alpha=0.88,\n            border_line_color=INK_SOFT,\n            border_line_alpha=0.35,\n            padding=10,\n        )\n    )\n\n# Custom JS hover formatters — distinctively Bokeh: show runner count alongside percentage\nfmt_time = CustomJSHover(code=\"return value.toFixed(0) + ' min'\")\nfmt_pct = CustomJSHover(\n    code=f\"\"\"\n    const pct = (value * 100).toFixed(1);\n    const runners = Math.round(value * {n_runners});\n    return pct + '% — ' + runners + '/{n_runners} runners';\n\"\"\"\n)\n\n# Interactive tools — Bokeh strengths\np.add_tools(\n    HoverTool(\n        renderers=[step_renderer],\n        tooltips=[(\"Finish Time\", \"@x{custom}\"), (\"Cumulative\", \"@y{custom}\")],\n        formatters={\"@x\": fmt_time, \"@y\": fmt_pct},\n        mode=\"vline\",\n    )\n)\np.add_tools(CrosshairTool(dimensions=\"both\", line_color=INK_SOFT, line_alpha=0.45))\n\n# Typography — canonical Bokeh sizing for 3200×1800\np.title.text_font_size = \"50pt\"\np.title.text_color = INK\np.title.text_font_style = \"bold\"\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# Chrome colors\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\np.xaxis.minor_tick_line_color = None\np.yaxis.minor_tick_line_color = None\n\n# Grid: y-only, subtle; no box outline\np.outline_line_color = None\np.xgrid.grid_line_color = None\np.ygrid.grid_line_color = INK\np.ygrid.grid_line_alpha = 0.10\n\n# Save — HTML artifact, then screenshot via headless Chrome (Selenium 4)\noutput_file(f\"plot-{THEME}.html\", title=title)\nsave(p)\n\n# H+200 gives Chrome UI overhead headroom; PIL crops to exact canvas dims after.\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()\nImage.open(io.BytesIO(raw)).crop((0, 0, W, H)).save(f\"plot-{THEME}.png\")\n"}