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
Selected Reading
How to fill an area within a polygon in Python using matplotlib?
Matplotlib provides several ways to fill areas within polygons. The most common approaches are using Polygon patches, fill() method, or PatchCollection for multiple polygons.
Method 1: Using fill() Method
The simplest way to fill a polygon is using matplotlib's fill() method ?
import matplotlib.pyplot as plt
import numpy as np
# Define polygon vertices (triangle)
x = [1, 4, 2]
y = [1, 2, 4]
plt.figure(figsize=(8, 6))
plt.fill(x, y, color='lightblue', alpha=0.7, edgecolor='blue')
plt.title('Filled Triangle Polygon')
plt.grid(True, alpha=0.3)
plt.show()
Method 2: Using Polygon Patch
For more control over polygon properties, use Polygon patches ?
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import numpy as np
# Create random pentagon vertices
vertices = np.array([[1, 1], [3, 1], [4, 3], [2, 4], [0.5, 2.5]])
fig, ax = plt.subplots(figsize=(8, 6))
# Create polygon patch
polygon = Polygon(vertices, closed=True, facecolor='lightgreen',
edgecolor='darkgreen', alpha=0.8, linewidth=2)
ax.add_patch(polygon)
ax.set_xlim(0, 5)
ax.set_ylim(0, 5)
ax.set_title('Filled Pentagon Using Polygon Patch')
ax.grid(True, alpha=0.3)
plt.show()
Method 3: Using PatchCollection for Multiple Polygons
When working with multiple polygons, PatchCollection provides better performance ?
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
import numpy as np
fig, ax = plt.subplots(figsize=(10, 8))
# Create multiple polygons
polygons = []
colors = []
for i in range(5):
# Generate random polygon vertices
vertices = np.random.rand(6, 2) * 4 + i
polygon = Polygon(vertices, closed=True)
polygons.append(polygon)
colors.append(i)
# Create patch collection
collection = PatchCollection(polygons, alpha=0.7, cmap='viridis')
collection.set_array(np.array(colors))
ax.add_collection(collection)
ax.set_xlim(0, 8)
ax.set_ylim(0, 6)
ax.set_title('Multiple Filled Polygons')
plt.colorbar(collection, ax=ax)
plt.show()
Customizing Polygon Appearance
You can customize colors, transparency, and edge properties ?
import matplotlib.pyplot as plt
import numpy as np
# Create star-shaped polygon
angles = np.linspace(0, 2*np.pi, 10, endpoint=False)
radii = [1 if i % 2 == 0 else 0.5 for i in range(10)]
x = radii * np.cos(angles)
y = radii * np.sin(angles)
plt.figure(figsize=(8, 8))
plt.fill(x, y, color='gold', alpha=0.8, edgecolor='orange',
linewidth=3, linestyle='--')
plt.title('Customized Star Polygon')
plt.axis('equal')
plt.grid(True, alpha=0.3)
plt.show()
Comparison of Methods
| Method | Best For | Performance | Customization |
|---|---|---|---|
fill() |
Simple polygons | Fast | Basic |
Polygon |
Single complex polygon | Medium | High |
PatchCollection |
Multiple polygons | Best for many | Advanced |
Conclusion
Use fill() for simple polygons, Polygon patches for detailed customization, and PatchCollection when working with multiple polygons. Each method offers different levels of control and performance optimization.
Advertisements
