Matplotlib
3D Plot
Render surfaces and scatter in 3D.
By EZ4Code Team
3dsurface
Code
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()Explanation
An Axes with projection='3d' supports surfaces, scatter, and wireframes in three dimensions. meshgrid creates the coordinate grids needed by plot_surface, while a colormap colors by height. Independent 3D panels live side by side in the same figure with subplots.
More Matplotlib Snippets
Line Plot
Plot lines with markers, colors, and styles.
Bar Chart
Draw vertical, horizontal, and grouped bars.
Scatter Plot
Visualize relationships with size and color encodings.
Subplots
Arrange multiple axes with subplots and GridSpec.
Labels and Legend
Annotate plots with text, arrows, and legend options.
Styles and Themes
Apply built-in styles and customize rcParams.