3D Scatter
DBSCAN Density-Based Clustering
DBSCAN clustering result showing dense clusters of varying shapes and noise points.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(246)
# DBSCAN-style clustering with noise
n_points = 300
# Dense cluster 1 (sphere)
n1 = 80
x1 = np.random.normal(0, 0.5, n1)
y1 = np.random.normal(0, 0.5, n1)
z1 = np.random.normal(0, 0.5, n1)
# Dense cluster 2 (elongated)
n2 = 80
t = np.random.uniform(0, 2*np.pi, n2)
x2 = np.cos(t) * 2 + 3 + np.random.normal(0, 0.2, n2)
y2 = np.sin(t) * 2 + np.random.normal(0, 0.2, n2)
z2 = t / np.pi - 1 + np.random.normal(0, 0.2, n2)
# Sparse cluster 3
n3 = 60
x3 = np.random.normal(-3, 0.8, n3)
y3 = np.random.normal(2, 0.8, n3)
z3 = np.random.normal(1, 0.8, n3)
# Noise
n_noise = 80
x_noise = np.random.uniform(-5, 5, n_noise)
y_noise = np.random.uniform(-3, 4, n_noise)
z_noise = np.random.uniform(-2, 3, n_noise)
fig = plt.figure(figsize=(10, 8), facecolor='#0a0a0f')
ax = fig.add_subplot(111, projection='3d', facecolor='#0a0a0f')
ax.scatter(x1, y1, z1, c='#06b6d4', s=40, alpha=0.8, label='Cluster 1')
ax.scatter(x2, y2, z2, c='#22c55e', s=40, alpha=0.8, label='Cluster 2')
ax.scatter(x3, y3, z3, c='#f59e0b', s=40, alpha=0.8, label='Cluster 3')
ax.scatter(x_noise, y_noise, z_noise, c='#64748b', s=20, alpha=0.4, label='Noise')
ax.set_xlabel('X', color='white', fontsize=10)
ax.set_ylabel('Y', color='white', fontsize=10)
ax.set_zlabel('Z', color='white', fontsize=10)
ax.set_title('DBSCAN Density-Based Clustering', color='white', fontsize=14, fontweight='bold', pad=20)
ax.legend(loc='upper right', fontsize=8, facecolor='#1e293b', edgecolor='#334155', labelcolor='white')
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=25, azim=45)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
3D Charts
☕