{"spec_id":"frequency-polygon-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nfrequency-polygon-basic: Frequency Polygon for Distribution Comparison\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-17\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\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Data - Three groups with different distributions\nnp.random.seed(42)\n\n# Group A: Normal distribution centered at 65 (Morning Session)\ngroup_a = np.random.normal(loc=65, scale=8, size=300)\n\n# Group B: Normal distribution centered at 75, more spread (Afternoon Session)\ngroup_b = np.random.normal(loc=75, scale=12, size=300)\n\n# Group C: Slightly bimodal distribution (Evening Session)\ngroup_c = np.concatenate([np.random.normal(loc=50, scale=6, size=150), np.random.normal(loc=60, scale=6, size=150)])\n\n# Common bin edges for all groups\nall_data = np.concatenate([group_a, group_b, group_c])\nbins = np.linspace(all_data.min() - 5, all_data.max() + 5, 21)\nbin_centers = (bins[:-1] + bins[1:]) / 2\n\n# Compute histogram counts\ncounts_a, _ = np.histogram(group_a, bins=bins)\ncounts_b, _ = np.histogram(group_b, bins=bins)\ncounts_c, _ = np.histogram(group_c, bins=bins)\n\n# Extend to zero at both ends for closed polygon shape\nbin_width = bins[1] - bins[0]\nx_extended = np.concatenate([[bin_centers[0] - bin_width], bin_centers, [bin_centers[-1] + bin_width]])\ny_a_extended = np.concatenate([[0], counts_a, [0]])\ny_b_extended = np.concatenate([[0], counts_b, [0]])\ny_c_extended = np.concatenate([[0], counts_c, [0]])\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"frequency-polygon-basic · bokeh · anyplot.ai\",\n    x_axis_label=\"Test Score (points)\",\n    y_axis_label=\"Frequency (count)\",\n)\n\n# Create sources\nsource_a = ColumnDataSource(data={\"x\": x_extended, \"y\": y_a_extended})\nsource_b = ColumnDataSource(data={\"x\": x_extended, \"y\": y_b_extended})\nsource_c = ColumnDataSource(data={\"x\": x_extended, \"y\": y_c_extended})\n\n# Plot frequency polygons with fills\n# Group A - Okabe-Ito 1 (Morning Session)\np.patch(x=\"x\", y=\"y\", source=source_a, fill_alpha=0.25, fill_color=IMPRINT[0], line_width=0)\np.line(x=\"x\", y=\"y\", source=source_a, line_color=IMPRINT[0], line_width=3, legend_label=\"Morning Session\")\np.scatter(x=bin_centers, y=counts_a, size=15, color=IMPRINT[0], alpha=0.9)\n\n# Group B - Okabe-Ito 2 (Afternoon Session)\np.patch(x=\"x\", y=\"y\", source=source_b, fill_alpha=0.25, fill_color=IMPRINT[1], line_width=0)\np.line(x=\"x\", y=\"y\", source=source_b, line_color=IMPRINT[1], line_width=3, legend_label=\"Afternoon Session\")\np.scatter(x=bin_centers, y=counts_b, size=15, color=IMPRINT[1], alpha=0.9)\n\n# Group C - Okabe-Ito 3 (Evening Session)\np.patch(x=\"x\", y=\"y\", source=source_c, fill_alpha=0.25, fill_color=IMPRINT[2], line_width=0)\np.line(x=\"x\", y=\"y\", source=source_c, line_color=IMPRINT[2], line_width=3, legend_label=\"Evening Session\")\np.scatter(x=bin_centers, y=counts_c, size=15, color=IMPRINT[2], alpha=0.9)\n\n# Theme-adaptive styling\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.title.text_font_size = \"28pt\"\n\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\n\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\n\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.location = \"top_right\"\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.padding = 15\np.legend.spacing = 8\np.legend.glyph_height = 35\np.legend.glyph_width = 35\np.legend.margin = 20\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"}