Tuesday, 25 March 2025

Voronoi Diagram using Python

 


import numpy as np

import matplotlib.pyplot as plt

from scipy.spatial import Voronoi, voronoi_plot_2d

np.random.seed(42)

num_points = 15

points = np.random.rand(num_points, 2)  

vor = Voronoi(points)

fig, ax = plt.subplots(figsize=(8, 6))

voronoi_plot_2d(vor, ax=ax, show_vertices=False, line_colors="blue", line_width=1.5)

ax.scatter(points[:, 0], points[:, 1], color="red", marker="o", label="Original Points")

plt.title("Voronoi Diagram")

plt.xlabel("X")

plt.ylabel("Y")

plt.legend()

plt.grid()

plt.show()

#source code --> clcoding.com


Code Explanation:

Import Required Libraries

import numpy as np

import matplotlib.pyplot as plt

from scipy.spatial import Voronoi, voronoi_plot_2d

numpy → Used to generate random points.

matplotlib.pyplot → Used to plot the Voronoi diagram.

scipy.spatial.Voronoi → Computes the Voronoi diagram.

scipy.spatial.voronoi_plot_2d → Helps in visualizing the Voronoi structure.

 Generate Random Points

np.random.seed(42)  # Set seed for reproducibility

num_points = 15  # Number of random points

points = np.random.rand(num_points, 2)  # Generate 15 random points in 2D (x, y)

np.random.seed(42) → Ensures that the random points are the same every time the code runs.

num_points = 15 → Defines 15 points for the Voronoi diagram.

np.random.rand(num_points, 2) → Generates 15 random points with x and y coordinates between [0,1]. 

Compute Voronoi Diagram

vor = Voronoi(points)

Computes the Voronoi diagram based on the input points.

The Voronoi diagram partitions the space so that each region contains all locations closest to one given point.

Create the Plot

fig, ax = plt.subplots(figsize=(8, 6))

Creates a figure (fig) and an axis (ax) for plotting.

figsize=(8,6) sets the figure size to 8 inches wide and 6 inches tall. 

Plot the Voronoi Diagram

voronoi_plot_2d(vor, ax=ax, show_vertices=False, line_colors="blue", line_width=1.5)

voronoi_plot_2d(vor, ax=ax) → Plots the Voronoi diagram on the given axis.

show_vertices=False → Hides the Voronoi vertices (points where the regions meet).

line_colors="blue" → Sets the region boundaries to blue.

line_width=1.5 → Makes the region boundaries thicker for better visibility.

Plot the Original Points

ax.scatter(points[:, 0], points[:, 1], color="red", marker="o", label="Original Points")

Plots the original 15 random points using red circular markers (o).

points[:, 0] → Extracts the x-coordinates of the points.

points[:, 1] → Extracts the y-coordinates of the points.

label="Original Points" → Adds a legend entry for the points.

 Customize the Plot

plt.title("Voronoi Diagram")  # Set title

plt.xlabel("X")  # X-axis label

plt.ylabel("Y")  # Y-axis label

plt.legend()  # Show legend

plt.grid()  # Add grid lines

Adds a title, axis labels, a legend, and grid lines for clarity.

 Display the Plot

plt.show()

Displays the final Voronoi diagram.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (197) C (77) C# (12) C++ (83) Course (67) Coursera (249) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (148) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1041) Python Coding Challenge (454) Python Quiz (116) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)