{"spec_id":"histogram-cumulative","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nhistogram-cumulative: Cumulative Histogram\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 95/100 | Updated: 2026-05-11\n\"\"\"\n\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\nimport numpy as np\n\n\nsys.path.pop(0)\n\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\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\"\n\n# Data - simulating response times (milliseconds) for a web service\nnp.random.seed(42)\nresponse_times = np.concatenate(\n    [np.random.exponential(scale=150, size=400), np.random.normal(loc=500, scale=100, size=100)]\n)\nresponse_times = np.clip(response_times, 10, 1000)\n\n# Calculate cumulative histogram\nn_bins = 30\nhist_counts, bin_edges = np.histogram(response_times, bins=n_bins)\ncumulative_counts = np.cumsum(hist_counts)\n\n# Prepare step data for cumulative histogram\nstep_x = []\nstep_y = []\nstep_x.append(bin_edges[0])\nstep_y.append(0)\nfor i in range(len(cumulative_counts)):\n    step_x.append(bin_edges[i])\n    step_y.append(cumulative_counts[i])\n    step_x.append(bin_edges[i + 1])\n    step_y.append(cumulative_counts[i])\n\nsource = ColumnDataSource(data={\"x\": step_x, \"y\": step_y})\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"histogram-cumulative · bokeh · anyplot.ai\",\n    x_axis_label=\"Response Time (ms)\",\n    y_axis_label=\"Cumulative Count\",\n    toolbar_location=None,\n)\n\n# Plot cumulative histogram as step function\np.line(x=\"x\", y=\"y\", source=source, line_width=5, color=BRAND, alpha=0.9)\n\n# Add filled area under the curve\nfill_x = step_x + [step_x[-1], step_x[0]]\nfill_y = step_y + [0, 0]\nfill_source = ColumnDataSource(data={\"x\": fill_x, \"y\": fill_y})\np.patch(x=\"x\", y=\"y\", source=fill_source, fill_color=BRAND, fill_alpha=0.15, line_width=0)\n\n# Add markers at bin edges\nmarker_source = ColumnDataSource(data={\"x\": bin_edges[1:], \"y\": cumulative_counts})\np.scatter(x=\"x\", y=\"y\", source=marker_source, size=15, color=BRAND, line_color=INK_SOFT, line_width=2, alpha=0.8)\n\n# Style text - scaled for 4800x2700\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# Theme-adaptive chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\np.outline_line_width = 1\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# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless 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)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}