Page12/13
3D Plotting & Advanced Visualizations Β· Page 1 of 1
3D Plots with mplot3d
3D Plotting & Advanced Visualizations
Importing 3D Tools
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
3D Scatter Plot
ax.scatter(x, y, z, c=colors, cmap='viridis', s=100)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
3D Surface Plot
Visualize mathematical functions or gridded data:
X, Y = np.meshgrid(x_vals, y_vals)
Z = X**2 + Y**2 # or any function
ax.plot_surface(X, Y, Z, cmap='coolwarm', alpha=0.8)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
3D Wireframe
Same as surface but shows the grid:
ax.plot_wireframe(X, Y, Z, color='#8b5cf6', linewidth=0.5)
When to use: 3D plots are impressive but hard to read in 2D (on paper/screen). Use 2D projections with color when possible.
main.py
Loading...
OUTPUT
βΆClick "Run Code" to executeβ¦