{"spec_id":"voronoi-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nvoronoi-basic: Voronoi Diagram for Spatial Partitioning\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-17\n\"\"\"\n\nimport os\nimport sys\n\n\nif sys.path[0] == os.path.dirname(os.path.abspath(__file__)):\n    sys.path.pop(0)\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib.patches import Polygon\nfrom scipy.spatial import Voronoi\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\"\n\n# Okabe-Ito palette (canonical order) — define as seaborn-compatible list\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"]\n\n# Data generation with spatial context\nnp.random.seed(42)\nn_points = 20\nx = np.random.uniform(10, 90, n_points)\ny = np.random.uniform(10, 90, n_points)\npoints = np.column_stack([x, y])\nbbox = [0, 100, 0, 100]\n\n# Mirror points for infinite region handling\nmargin = 200\nmirror_points = []\nfor px, py in points:\n    mirror_points.extend([[px, -margin], [px, 100 + margin], [-margin, py], [100 + margin, py]])\nall_points = np.vstack([points, mirror_points])\n\n# Compute Voronoi tessellation\nvor = Voronoi(all_points)\n\n# Configure seaborn with sophisticated theming\nsns.set_theme(style=\"ticks\")\nsns.set_palette(IMPRINT)  # Set global palette to Okabe-Ito\nsns.set_context(\"talk\", font_scale=1.1)  # Enhanced font sizing via seaborn context\n\n# Apply theme-adaptive rendering context\nplt.rcParams.update(\n    {\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    }\n)\n\n# Create figure\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Get seaborn's color palette for dynamic color cycling\npal = sns.color_palette(IMPRINT)\n\n# Draw Voronoi regions with dynamic color cycling from seaborn palette\nfor i in range(n_points):\n    region_idx = vor.point_region[i]\n    region = vor.regions[region_idx]\n    if not region or -1 in region:\n        continue\n\n    polygon_vertices = vor.vertices[region].copy()\n    polygon_vertices[:, 0] = np.clip(polygon_vertices[:, 0], bbox[0], bbox[1])\n    polygon_vertices[:, 1] = np.clip(polygon_vertices[:, 1], bbox[2], bbox[3])\n\n    # Use seaborn's palette cycling for color selection\n    color = pal[i % len(pal)]\n    poly = Polygon(polygon_vertices, facecolor=color, edgecolor=INK_SOFT, linewidth=2.5, alpha=0.7)\n    ax.add_patch(poly)\n\n# Draw Voronoi edges with seaborn-styled aesthetics\nfor ridge_idx, (p1, p2) in enumerate(vor.ridge_points):\n    if p1 < n_points and p2 < n_points:\n        v1, v2 = vor.ridge_vertices[ridge_idx]\n        if v1 >= 0 and v2 >= 0:\n            x_coords = np.clip([vor.vertices[v1, 0], vor.vertices[v2, 0]], bbox[0], bbox[1])\n            y_coords = np.clip([vor.vertices[v1, 1], vor.vertices[v2, 1]], bbox[2], bbox[3])\n            ax.plot(x_coords, y_coords, color=INK_SOFT, linewidth=2.5, alpha=0.9)\n\n# Plot seed points with enhanced seaborn styling\ndf = pd.DataFrame({\"x\": x, \"y\": y})\nsns.scatterplot(data=df, x=\"x\", y=\"y\", s=350, color=BRAND, edgecolor=INK, linewidth=2.5, ax=ax, zorder=10)\n\n# Labels with seaborn-compatible font sizing\nax.set_xlabel(\"X Coordinate (km)\", fontsize=20, color=INK)\nax.set_ylabel(\"Y Coordinate (km)\", fontsize=20, color=INK)\nax.set_title(\"voronoi-basic · seaborn · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK, pad=20)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.set_xlim(bbox[0], bbox[1])\nax.set_ylim(bbox[2], bbox[3])\nax.set_aspect(\"equal\")\n\n# Spine styling with seaborn theme-adaptive colors\nfor spine in ax.spines.values():\n    spine.set_edgecolor(INK_SOFT)\n    spine.set_linewidth(2)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}