Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Differentiate the orthographic and perspective projection in Matplotlib
In Matplotlib 3D plotting, you can use two different projection types: perspective and orthographic. Perspective projection simulates how objects appear to the human eye with depth perception, while orthographic projection maintains parallel lines and consistent object sizes regardless of distance.
Key Differences
| Aspect | Perspective Projection | Orthographic Projection |
|---|---|---|
| Depth Perception | Objects farther away appear smaller | Objects maintain size regardless of distance |
| Parallel Lines | Converge to vanishing points | Remain parallel |
| Use Case | Realistic 3D visualization | Technical drawings, measurements |
Example: Comparing Both Projections
Here's how to create side-by-side plots showing both projection types ?
import matplotlib.pyplot as plt
import numpy as np
# Set figure parameters
plt.rcParams["figure.figsize"] = [10, 5]
plt.rcParams["figure.autolayout"] = True
# Create sample 3D data
x = np.array([0, 1, 1, 0, 0, 1, 1, 0])
y = np.array([0, 0, 1, 1, 0, 0, 1, 1])
z = np.array([0, 0, 0, 0, 1, 1, 1, 1])
fig = plt.figure()
# Perspective projection
ax1 = fig.add_subplot(1, 2, 1, projection='3d')
ax1.set_proj_type('persp')
ax1.scatter(x, y, z, c='red', s=100)
ax1.set_title('Perspective Projection')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
ax1.set_zlabel('Z')
# Orthographic projection
ax2 = fig.add_subplot(1, 2, 2, projection='3d')
ax2.set_proj_type('ortho')
ax2.scatter(x, y, z, c='blue', s=100)
ax2.set_title('Orthographic Projection')
ax2.set_xlabel('X')
ax2.set_ylabel('Y')
ax2.set_zlabel('Z')
plt.show()
Setting Projection Types
You can set the projection type using the set_proj_type() method ?
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
# For perspective projection
ax.set_proj_type('persp')
# For orthographic projection
ax.set_proj_type('ortho')
plt.show()
Practical Applications
Perspective projection is ideal for:
- Data visualization that mimics human vision
- Creating realistic 3D plots
- Scientific presentations requiring depth perception
Orthographic projection is better for:
- Technical drawings and blueprints
- Precise measurements and comparisons
- Engineering and architectural visualizations
Conclusion
Choose perspective projection for realistic 3D visualization and orthographic projection for technical accuracy. The set_proj_type() method with 'persp' or 'ortho' parameters controls which projection type to use.
