3D Voxels
Voxel Torus
Donut-shaped torus approximation with pink-magenta coloring.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
n = 16
x, y, z = np.indices((n, n, n))
# Torus parameters
center = n // 2
R = 5 # Major radius
r = 2 # Minor radius
dist_xy = np.sqrt((x - center)**2 + (y - center)**2)
dist_torus = np.sqrt((dist_xy - R)**2 + (z - center)**2)
torus = dist_torus <= r
# Gradient: pink #F527B0 to coral #F5276C
colors = np.empty(torus.shape + (4,), dtype=np.float32)
angle = np.arctan2(y - center, x - center)
colors[..., 0] = 0.96 # R
colors[..., 1] = 0.15 + 0.12 * (np.sin(angle) + 1) / 2 # G
colors[..., 2] = 0.69 - 0.27 * (np.cos(angle) + 1) / 2 # B
colors[..., 3] = 0.9
fig = plt.figure(figsize=(10, 8), facecolor='#020B14')
ax = fig.add_subplot(111, projection='3d', facecolor='#020B14')
ax.voxels(torus, facecolors=colors, edgecolor='#ffffff15', linewidth=0.15)
ax.set_xlabel('X', fontsize=10, color='#94a3b8', labelpad=10)
ax.set_ylabel('Y', fontsize=10, color='#94a3b8', labelpad=10)
ax.set_zlabel('Z', fontsize=10, color='#94a3b8', labelpad=10)
ax.set_title("Voxel Torus", fontsize=14, color='white', fontweight='bold', pad=20)
ax.tick_params(colors='#64748b', labelsize=8)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
ax.xaxis.pane.set_edgecolor('#1e293b')
ax.yaxis.pane.set_edgecolor('#1e293b')
ax.zaxis.pane.set_edgecolor('#1e293b')
ax.view_init(elev=35, azim=45)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
3D Charts
More 3D Voxels examples
☕