{"spec_id":"voronoi-basic","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nvoronoi-basic: Voronoi Diagram for Spatial Partitioning\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 90/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 scipy.spatial import Voronoi\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\"  # Okabe-Ito position 1\n\n# Okabe-Ito palette for Voronoi cells\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"]\n\n# Data - seed points for Voronoi diagram\nnp.random.seed(42)\nn_points = 20\nx = np.random.uniform(1, 9, n_points)\ny = np.random.uniform(1, 9, n_points)\npoints = np.column_stack([x, y])\n\n# Bounding box for clipping\nx_min, x_max = 0, 10\ny_min, y_max = 0, 10\n\n# Add mirrored points outside boundaries to handle infinite regions properly\nmirrored = []\nfor px, py in points:\n    mirrored.append([2 * x_min - px, py])\n    mirrored.append([2 * x_max - px, py])\n    mirrored.append([px, 2 * y_min - py])\n    mirrored.append([px, 2 * y_max - py])\nall_points = np.vstack([points, mirrored])\n\n# Compute Voronoi diagram\nvor = Voronoi(all_points)\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"voronoi-basic · bokeh · anyplot.ai\",\n    x_axis_label=\"X Coordinate\",\n    y_axis_label=\"Y Coordinate\",\n    x_range=(x_min, x_max),\n    y_range=(y_min, y_max),\n)\n\n# Draw Voronoi regions for original points only\nfor idx in range(n_points):\n    region_idx = vor.point_region[idx]\n    region = vor.regions[region_idx]\n\n    if not region or -1 in region:\n        continue\n\n    vertices = [vor.vertices[i] for i in region]\n\n    # Clip polygon to bounding box using Sutherland-Hodgman algorithm\n    polygon = list(vertices)\n    for edge in [\"left\", \"right\", \"bottom\", \"top\"]:\n        if not polygon:\n            break\n        clipped = []\n        for i in range(len(polygon)):\n            curr = polygon[i]\n            next_v = polygon[(i + 1) % len(polygon)]\n            curr_in = (\n                (curr[0] >= x_min if edge == \"left\" else True)\n                and (curr[0] <= x_max if edge == \"right\" else True)\n                and (curr[1] >= y_min if edge == \"bottom\" else True)\n                and (curr[1] <= y_max if edge == \"top\" else True)\n            )\n            next_in = (\n                (next_v[0] >= x_min if edge == \"left\" else True)\n                and (next_v[0] <= x_max if edge == \"right\" else True)\n                and (next_v[1] >= y_min if edge == \"bottom\" else True)\n                and (next_v[1] <= y_max if edge == \"top\" else True)\n            )\n            if curr_in:\n                clipped.append(curr)\n                if not next_in:\n                    x1, y1 = curr\n                    x2, y2 = next_v\n                    dx, dy = x2 - x1, y2 - y1\n                    if edge == \"left\":\n                        t = (x_min - x1) / dx if dx != 0 else 0\n                        clipped.append([x_min, y1 + t * dy])\n                    elif edge == \"right\":\n                        t = (x_max - x1) / dx if dx != 0 else 0\n                        clipped.append([x_max, y1 + t * dy])\n                    elif edge == \"bottom\":\n                        t = (y_min - y1) / dy if dy != 0 else 0\n                        clipped.append([x1 + t * dx, y_min])\n                    elif edge == \"top\":\n                        t = (y_max - y1) / dy if dy != 0 else 0\n                        clipped.append([x1 + t * dx, y_max])\n            elif next_in:\n                x1, y1 = curr\n                x2, y2 = next_v\n                dx, dy = x2 - x1, y2 - y1\n                if edge == \"left\":\n                    t = (x_min - x1) / dx if dx != 0 else 0\n                    clipped.append([x_min, y1 + t * dy])\n                elif edge == \"right\":\n                    t = (x_max - x1) / dx if dx != 0 else 0\n                    clipped.append([x_max, y1 + t * dy])\n                elif edge == \"bottom\":\n                    t = (y_min - y1) / dy if dy != 0 else 0\n                    clipped.append([x1 + t * dx, y_min])\n                elif edge == \"top\":\n                    t = (y_max - y1) / dy if dy != 0 else 0\n                    clipped.append([x1 + t * dx, y_max])\n        polygon = clipped\n\n    if len(polygon) >= 3:\n        xs = [v[0] for v in polygon]\n        ys = [v[1] for v in polygon]\n        cell_color = IMPRINT[idx % len(IMPRINT)]\n        p.patch(xs, ys, fill_color=cell_color, fill_alpha=0.5, line_color=INK_SOFT, line_width=2.5)\n\n# Draw seed points prominently\nsource = ColumnDataSource(data={\"x\": x, \"y\": y})\np.scatter(\"x\", \"y\", source=source, size=22, color=BRAND, line_color=PAGE_BG, line_width=4, alpha=0.95)\n\n# Styling\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\n\np.xaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\n\np.xaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\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_SOFT\np.ygrid.grid_line_color = INK_SOFT\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\n\n# Save\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"}