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
Plot Matplotlib 3D plot_surface with contour plot projection
To create a 3D surface plot with contour projections in Matplotlib, we combine plot_surface() for the main surface and contourf() for projecting contours onto the coordinate planes.
Understanding the Components
A surface plot with contour projections consists of:
A 3D surface using
plot_surface()Contour projections on the XY, XZ, and YZ planes using
contourf()The
zdirparameter controls which plane the contour is projected onto
Basic Example
Here's how to create a surface plot with contour projections ?
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Set figure size
plt.rcParams["figure.figsize"] = [10, 8]
plt.rcParams["figure.autolayout"] = True
# Create data points
x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)
X, Y = np.meshgrid(x, y)
Z = X * np.exp(-X**2 - Y**2)
# Create 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Create surface plot
surf = ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.8, cmap="viridis")
# Add contour projections
ax.contourf(X, Y, Z, zdir='z', offset=np.min(Z), cmap="plasma", alpha=0.6)
ax.contourf(X, Y, Z, zdir='x', offset=-5, cmap="coolwarm", alpha=0.6)
ax.contourf(X, Y, Z, zdir='y', offset=5, cmap="spring", alpha=0.6)
# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_title('3D Surface with Contour Projections')
plt.show()
Understanding the Parameters
| Parameter | Description | Effect |
|---|---|---|
zdir='z' |
Project onto XY plane | Bottom projection |
zdir='x' |
Project onto YZ plane | Side projection |
zdir='y' |
Project onto XZ plane | Back projection |
Advanced Example with Customization
Create a more complex surface with better styling ?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Create data
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))
# Create figure and axis
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
# Main surface plot
surface = ax.plot_surface(X, Y, Z,
rstride=2, cstride=2,
alpha=0.7,
cmap='jet',
linewidth=0,
antialiased=True)
# Contour projections
contour_z = ax.contourf(X, Y, Z, zdir='z', offset=-1.2,
cmap='Reds', alpha=0.5, levels=10)
contour_x = ax.contourf(X, Y, Z, zdir='x', offset=-3,
cmap='Blues', alpha=0.5, levels=10)
contour_y = ax.contourf(X, Y, Z, zdir='y', offset=3,
cmap='Greens', alpha=0.5, levels=10)
# Customize the plot
ax.set_xlim(-3, 3)
ax.set_ylim(-3, 3)
ax.set_zlim(-1.2, 1.2)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Sine Wave Surface with Contour Projections')
# Add colorbar
plt.colorbar(surface, ax=ax, shrink=0.5)
plt.show()
Key Points
Use
meshgrid()to create coordinate arrays for 3D plottingThe
offsetparameter positions the contour projectionDifferent colormaps help distinguish between projections
Alpha transparency prevents projections from hiding the main surface
Use
rstrideandcstrideto control surface resolution
Conclusion
Combining plot_surface() with contourf() projections creates informative 3D visualizations. Use different colormaps and transparency to clearly distinguish between the surface and its projections on coordinate planes.
