{"spec_id":"histogram-density","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nhistogram-density: Density Histogram\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-11\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\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\"\n\n# Okabe-Ito palette (first series for histogram, second for PDF)\nCOLOR_HIST = \"#009E73\"\nCOLOR_PDF = \"#C475FD\"\n\n# Data - Test scores with normal-like distribution\nnp.random.seed(42)\nmu, sigma = 75, 12\nscores = np.random.normal(loc=mu, scale=sigma, size=500)\n\n# Calculate histogram with density normalization\nbin_edges = np.linspace(scores.min() - 5, scores.max() + 5, 31)\nhist_counts, edges = np.histogram(scores, bins=bin_edges, density=True)\nleft_edges = edges[:-1]\nright_edges = edges[1:]\n\n# Theoretical normal PDF for overlay\nx_pdf = np.linspace(scores.min() - 10, scores.max() + 10, 200)\npdf_values = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_pdf - mu) / sigma) ** 2)\n\n# Sources\nhist_source = ColumnDataSource(\n    data={\"left\": left_edges, \"right\": right_edges, \"top\": hist_counts, \"bottom\": [0] * len(hist_counts)}\n)\npdf_source = ColumnDataSource(data={\"x\": x_pdf, \"y\": pdf_values})\n\n# Create figure (4800 x 2700 px)\np = figure(\n    width=4800,\n    height=2700,\n    title=\"histogram-density · bokeh · anyplot.ai\",\n    x_axis_label=\"Test Score\",\n    y_axis_label=\"Density (Probability per Unit)\",\n)\n\n# Plot histogram bars\np.quad(\n    left=\"left\",\n    right=\"right\",\n    top=\"top\",\n    bottom=\"bottom\",\n    source=hist_source,\n    fill_color=COLOR_HIST,\n    fill_alpha=0.7,\n    line_color=PAGE_BG,\n    line_width=2,\n    legend_label=\"Empirical Distribution\",\n)\n\n# Plot theoretical PDF overlay\np.line(x=\"x\", y=\"y\", source=pdf_source, line_color=COLOR_PDF, line_width=5, legend_label=\"Normal PDF (μ=75, σ=12)\")\n\n# Styling for 4800x2700 px\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 colors\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\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\n\n# Subtle grid\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.label_text_font_size = \"20pt\"\np.legend.label_text_color = INK_SOFT\np.legend.location = \"top_left\"\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\n\n# Y-axis starts at zero\np.y_range.start = 0\n\n# Save interactive 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"}