{"spec_id":"histogram-stacked","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nhistogram-stacked: Stacked Histogram\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 84/100 | Updated: 2026-05-12\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\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\"\n\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Data - Simulating test scores from three different study groups\nnp.random.seed(42)\n\ngroup_a = np.random.normal(loc=75, scale=8, size=150)\ngroup_b = np.random.normal(loc=82, scale=10, size=120)\ngroup_c = np.random.normal(loc=65, scale=12, size=100)\n\n# Cap test scores at realistic range (0-100)\ngroup_a = np.clip(group_a, 0, 100)\ngroup_b = np.clip(group_b, 0, 100)\ngroup_c = np.clip(group_c, 0, 100)\n\n# Create consistent bin edges for all groups\nall_data = np.concatenate([group_a, group_b, group_c])\nbin_edges = np.linspace(0, 100, 16)\n\n# Compute histograms with same bins\nhist_a, _ = np.histogram(group_a, bins=bin_edges)\nhist_b, _ = np.histogram(group_b, bins=bin_edges)\nhist_c, _ = np.histogram(group_c, bins=bin_edges)\n\nbin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2\nbin_width = bin_edges[1] - bin_edges[0]\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"histogram-stacked · bokeh · anyplot.ai\",\n    x_axis_label=\"Test Score (points)\",\n    y_axis_label=\"Frequency (count)\",\n)\n\n# Prepare data for stacking\nsource_c = ColumnDataSource(\n    data={\n        \"x\": bin_centers,\n        \"top\": hist_c,\n        \"bottom\": np.zeros_like(hist_c),\n        \"width\": [bin_width * 0.85] * len(bin_centers),\n    }\n)\n\nsource_a = ColumnDataSource(\n    data={\"x\": bin_centers, \"top\": hist_c + hist_a, \"bottom\": hist_c, \"width\": [bin_width * 0.85] * len(bin_centers)}\n)\n\nsource_b = ColumnDataSource(\n    data={\n        \"x\": bin_centers,\n        \"top\": hist_c + hist_a + hist_b,\n        \"bottom\": hist_c + hist_a,\n        \"width\": [bin_width * 0.85] * len(bin_centers),\n    }\n)\n\n# Plot stacked bars using ColumnDataSource\np.vbar(\n    x=\"x\",\n    top=\"top\",\n    bottom=\"bottom\",\n    width=\"width\",\n    source=source_c,\n    fill_color=IMPRINT[2],\n    line_color=\"white\",\n    line_width=2,\n    alpha=0.9,\n    legend_label=\"Group C\",\n)\n\np.vbar(\n    x=\"x\",\n    top=\"top\",\n    bottom=\"bottom\",\n    width=\"width\",\n    source=source_a,\n    fill_color=IMPRINT[0],\n    line_color=\"white\",\n    line_width=2,\n    alpha=0.9,\n    legend_label=\"Group A\",\n)\n\np.vbar(\n    x=\"x\",\n    top=\"top\",\n    bottom=\"bottom\",\n    width=\"width\",\n    source=source_b,\n    fill_color=IMPRINT[1],\n    line_color=\"white\",\n    line_width=2,\n    alpha=0.9,\n    legend_label=\"Group B\",\n)\n\n# Text sizing for 4800x2700 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# Theme-adaptive chrome\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\n# Legend styling\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\np.legend.label_text_color = INK_SOFT\np.legend.label_text_font_size = \"18pt\"\np.legend.location = \"top_right\"\n\np.y_range.start = 0\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with Selenium\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"}