{"spec_id":"voronoi-basic","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nvoronoi-basic: Voronoi Diagram for Spatial Partitioning\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.collections import PolyCollection\nfrom scipy.spatial import Voronoi\n\n\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Okabe-Ito palette for Voronoi regions\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"]\n\n# Data - Generate seed points for Voronoi diagram\nnp.random.seed(42)\nn_points = 15\nx = np.random.uniform(1, 9, n_points)\ny = np.random.uniform(1, 9, n_points)\npoints = np.column_stack([x, y])\n\n# Create Voronoi tessellation with mirror points for bounded regions\n# Add mirror points outside the boundary to ensure all regions are finite\nx_min, x_max, y_min, y_max = 0, 10, 0, 10\nmirror_points = np.vstack(\n    [\n        points,\n        np.column_stack([x, 2 * y_min - y]),\n        np.column_stack([x, 2 * y_max - y]),\n        np.column_stack([2 * x_min - x, y]),\n        np.column_stack([2 * x_max - x, y]),\n    ]\n)\nvor = Voronoi(mirror_points)\n\n# Create figure with theme-aware background\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Collect polygons for original points only (first n_points)\npolygons = []\npoly_colors = []\n\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    # Get polygon vertices\n    polygon = np.array([vor.vertices[v] for v in region])\n\n    # Clip polygon to bounding box\n    polygon[:, 0] = np.clip(polygon[:, 0], x_min, x_max)\n    polygon[:, 1] = np.clip(polygon[:, 1], y_min, y_max)\n\n    polygons.append(polygon)\n    poly_colors.append(IMPRINT[idx % len(IMPRINT)])\n\n# Draw all Voronoi regions at once\ncollection = PolyCollection(polygons, facecolors=poly_colors, edgecolors=INK_SOFT, linewidths=2.5, alpha=0.6)\nax.add_collection(collection)\n\n# Plot seed points prominently with brand green\nax.scatter(x, y, s=350, c=\"#009E73\", edgecolors=\"white\", linewidths=3, zorder=5)\n\n# Set bounds and styling\nax.set_xlim(-0.2, 10.2)\nax.set_ylim(-0.2, 10.2)\nax.set_xlabel(\"X Coordinate\", fontsize=20, color=INK)\nax.set_ylabel(\"Y Coordinate\", fontsize=20, color=INK)\nax.set_title(\"voronoi-basic · matplotlib · anyplot.ai\", fontsize=24, color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.set_aspect(\"equal\")\nax.yaxis.grid(True, alpha=0.12, linewidth=0.8, color=INK)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}