Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to draw the largest polygon from a set of points in matplotlib?
To draw the largest polygon from a set of points in matplotlib, we need to find the convex hull of the points and then create a polygon patch. The convex hull gives us the outermost boundary that encloses all points, forming the largest possible polygon.
Understanding Convex Hull
A convex hull is the smallest convex polygon that contains all given points. Think of it as stretching a rubber band around all the points − the shape it forms is the convex hull.
Finding the Largest Polygon
We'll use scipy's ConvexHull to find the largest polygon from a set of points ?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from scipy.spatial import ConvexHull
# Set figure size
plt.rcParams["figure.figsize"] = [8.00, 6.00]
plt.rcParams["figure.autolayout"] = True
# Create a set of random points
points = np.array([[1, 1], [0.5, 1.5], [2, 1], [1, 2], [2, 2],
[1.5, 0.5], [0.8, 2.2], [2.5, 1.8], [0.3, 1.2]])
# Find the convex hull (largest polygon)
hull = ConvexHull(points)
hull_points = points[hull.vertices]
# Create the polygon
polygon = Polygon(hull_points, facecolor='lightblue',
edgecolor='blue', alpha=0.7, linewidth=2)
# Create figure and plot
fig, ax = plt.subplots()
# Add the polygon patch
ax.add_patch(polygon)
# Plot all points
ax.scatter(points[:, 0], points[:, 1], color='red', s=50, zorder=5)
# Plot hull vertices
ax.scatter(hull_points[:, 0], hull_points[:, 1], color='blue', s=80, zorder=6)
# Set axis limits
ax.set_xlim([0, 3])
ax.set_ylim([0, 3])
ax.set_xlabel('X coordinate')
ax.set_ylabel('Y coordinate')
ax.set_title('Largest Polygon (Convex Hull) from Set of Points')
ax.grid(True, alpha=0.3)
plt.show()
The plot displays: - A light blue polygon representing the convex hull - Red dots showing all input points - Blue dots highlighting the vertices of the largest polygon - Grid lines for better visualization
Simple Polygon Without Convex Hull
If you want to create a polygon directly from specific points without finding the convex hull ?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
# Define polygon vertices in order
vertices = np.array([[1, 1], [0.5, 1.5], [1, 2], [2, 2], [2, 1]])
# Create polygon patch
polygon = Polygon(vertices, facecolor='lightgreen',
edgecolor='darkgreen', alpha=0.8, linewidth=2)
# Create figure
fig, ax = plt.subplots(figsize=(7, 5))
# Add polygon to plot
ax.add_patch(polygon)
# Plot vertices
ax.scatter(vertices[:, 0], vertices[:, 1], color='red', s=60, zorder=5)
# Annotate vertices
for i, (x, y) in enumerate(vertices):
ax.annotate(f'P{i+1}', (x, y), xytext=(5, 5),
textcoords='offset points', fontsize=10)
ax.set_xlim([0, 3])
ax.set_ylim([0, 3])
ax.set_xlabel('X coordinate')
ax.set_ylabel('Y coordinate')
ax.set_title('Custom Polygon from Ordered Points')
ax.grid(True, alpha=0.3)
plt.show()
Displays a green polygon with: - Vertices marked as red dots - Labels P1, P2, etc. for each point - Grid for reference
Key Parameters
- facecolor − Interior color of the polygon
- edgecolor − Border color of the polygon
- alpha − Transparency level (0−1)
- linewidth − Thickness of the polygon border
Conclusion
Use scipy's ConvexHull to find the largest polygon from scattered points. For predefined shapes, create a Polygon patch directly with ordered vertices. Both approaches allow customization of colors, transparency, and styling.
