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
Transparency for Poly3DCollection plot in Matplotlib
A Poly3DCollection in Matplotlib allows you to create 3D polygonal surfaces. To make these surfaces transparent, you use the alpha parameter to control opacity levels between 0 (fully transparent) and 1 (fully opaque).
Basic Setup
First, let's create a simple 3D tetrahedron with transparent faces ?
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
import numpy as np
# Set up the figure
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
# Define vertices of a tetrahedron
x = [0, 2, 1, 1]
y = [0, 0, 1, 0]
z = [0, 0, 0, 1]
# Define which vertices form each face
vertices = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]]
# Convert to coordinate tuples
tupleList = list(zip(x, y, z))
poly3d = [[tupleList[vertices[ix][iy]]
for iy in range(len(vertices[0]))]
for ix in range(len(vertices))]
# Add transparent polygons
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='cyan', alpha=0.3))
# Add edge lines for clarity
ax.add_collection3d(Line3DCollection(poly3d, colors='black', linewidths=1))
# Add vertex points
ax.scatter(x, y, z, color='red', s=50)
plt.show()
Controlling Transparency Levels
Different alpha values create different transparency effects ?
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
fig, axes = plt.subplots(1, 3, figsize=(15, 5), subplot_kw={'projection': '3d'})
# Create a simple cube
vertices = np.array([
[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], # bottom face
[0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1] # top face
])
# Define the 6 faces of the cube
faces = [
[vertices[j] for j in [0, 1, 2, 3]], # bottom
[vertices[j] for j in [4, 5, 6, 7]], # top
[vertices[j] for j in [0, 1, 5, 4]], # front
[vertices[j] for j in [2, 3, 7, 6]], # back
[vertices[j] for j in [0, 3, 7, 4]], # left
[vertices[j] for j in [1, 2, 6, 5]] # right
]
alpha_values = [0.2, 0.5, 0.8]
titles = ['Low Transparency (?=0.2)', 'Medium Transparency (?=0.5)', 'High Transparency (?=0.8)']
for i, (ax, alpha, title) in enumerate(zip(axes, alpha_values, titles)):
# Add the cube with different alpha values
ax.add_collection3d(Poly3DCollection(faces, facecolors='blue', alpha=alpha, edgecolors='black'))
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
ax.set_title(title)
plt.tight_layout()
plt.show()
Multiple Overlapping Transparent Objects
Transparency is especially useful when displaying multiple overlapping 3D objects ?
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Create two overlapping pyramids
def create_pyramid(center, size):
cx, cy, cz = center
vertices = [
[cx-size, cy-size, cz], # base corners
[cx+size, cy-size, cz],
[cx+size, cy+size, cz],
[cx-size, cy+size, cz],
[cx, cy, cz+size*2] # apex
]
faces = [
[vertices[j] for j in [0, 1, 2, 3]], # base
[vertices[j] for j in [0, 1, 4]], # side faces
[vertices[j] for j in [1, 2, 4]],
[vertices[j] for j in [2, 3, 4]],
[vertices[j] for j in [3, 0, 4]]
]
return faces
# Create two pyramids at different positions
pyramid1 = create_pyramid((0, 0, 0), 1)
pyramid2 = create_pyramid((0.5, 0.5, 0.5), 1)
# Add first pyramid (red, semi-transparent)
ax.add_collection3d(Poly3DCollection(pyramid1, facecolors='red', alpha=0.4, edgecolors='darkred'))
# Add second pyramid (blue, semi-transparent)
ax.add_collection3d(Poly3DCollection(pyramid2, facecolors='blue', alpha=0.4, edgecolors='darkblue'))
ax.set_xlim(-2, 3)
ax.set_ylim(-2, 3)
ax.set_zlim(0, 4)
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('Overlapping Transparent Pyramids')
plt.show()
Key Parameters
| Parameter | Description | Range |
|---|---|---|
alpha |
Controls transparency | 0.0 (transparent) to 1.0 (opaque) |
facecolors |
Fill color of faces | Color names, hex codes, RGB tuples |
edgecolors |
Color of polygon edges | Color names, hex codes, RGB tuples |
linewidths |
Width of edge lines | Positive numbers |
Conclusion
Use the alpha parameter in Poly3DCollection to create transparent 3D surfaces. Lower alpha values increase transparency, making it easier to visualize overlapping or internal structures in 3D plots.
Advertisements
