3D Voxels
Torus Knot Voxels
Torus knot (trefoil) approximation with purple-cyan gradient.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
n = 15
x, y, z = np.indices((n, n, n))
cx, cy, cz = n // 2, n // 2, n // 2
# Diamond: octahedron stretched vertically
r = 5
diamond = (np.abs(x - cx) + np.abs(y - cy) + np.abs(z - cz) * 0.7) <= r
# Gradient from deep_purple #5314E6 to purple #4927F5
colors = np.empty(diamond.shape + (4,), dtype=np.float32)
colors[..., 3] = 0
norm_z = z / (n - 1)
# 5314E6 -> 4927F5
colors[diamond, 0] = 0.33 + 0.16 * norm_z[diamond] # 53 to 49
colors[diamond, 1] = 0.08 + 0.07 * norm_z[diamond] # 14 to 27
colors[diamond, 2] = 0.90 + 0.06 * norm_z[diamond] # E6 to F5
colors[diamond, 3] = 0.88
fig = plt.figure(figsize=(10, 8), facecolor='#020B14')
ax = fig.add_subplot(111, projection='3d', facecolor='#020B14')
ax.voxels(diamond, facecolors=colors, edgecolors='#1e293b', linewidth=0.2)
ax.set_xlabel('X', fontsize=11, color='#94a3b8', labelpad=10)
ax.set_ylabel('Y', fontsize=11, color='#94a3b8', labelpad=10)
ax.set_zlabel('Z', fontsize=11, color='#94a3b8', labelpad=10)
ax.set_title('Diamond Voxels', 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.grid(True, alpha=0.2, color='#475569')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
3D Charts
More 3D Voxels examples
☕