Skip to content
Matplotlib

3D Plot

Render surfaces and scatter in 3D.

#3d#surface

Code

matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8, 6))

# 3D surface
ax = fig.add_subplot(121, projection="3d")
x = np.linspace(-3, 3, 50)
y = np.linspace(-3, 3, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X ** 2 + Y ** 2))
ax.plot_surface(X, Y, Z, cmap="viridis", edgecolor="none")
ax.set_title("Surface")
ax.set_xlabel("x"); ax.set_ylabel("y"); ax.set_zlabel("z")

# 3D scatter
ax2 = fig.add_subplot(122, projection="3d")
xs = np.random.rand(30) * 6 - 3
ys = np.random.rand(30) * 6 - 3
zs = np.sin(np.sqrt(xs ** 2 + ys ** 2))
ax2.scatter(xs, ys, zs, c=zs, cmap="plasma")
ax2.set_title("Scatter")

plt.tight_layout()
plt.show()