Layering a contourf plot and surface_plot in Matplotlib

Layering a contourf plot and surface plot in Matplotlib allows you to combine 2D filled contours with 3D surface visualization. This technique is useful for highlighting specific contour levels while showing the complete 3D structure of your data.

Step-by-Step Process

To create layered plots, follow these steps ?

  • Initialize variables for grid spacing and coordinate ranges using NumPy

  • Create meshgrid coordinates for the plotting domain

  • Create a 3D figure and axis with projection='3d'

  • Add the contour plot using contour() or contourf()

  • Layer the surface plot using plot_surface()

  • Display the combined visualization

Basic Example

Here's how to layer a contour plot with a surface plot ?

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

# Set up the grid
delta = 0.25
xrange = np.arange(-3.0, 3.0, delta)
yrange = np.arange(-3.0, 3.0, delta)
x, y = np.meshgrid(xrange, yrange)

# Define the function
z = np.sin(np.sqrt(x**2 + y**2))

# Create 3D plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Add contour lines at z=0 level
ax.contour(x, y, z, levels=[0], colors='red', linewidths=2)

# Add surface plot
surf = ax.plot_surface(x, y, z, cmap='viridis', alpha=0.7)

# Add labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Layered Contour and Surface Plot')

plt.show()

Using Filled Contours

You can also use filled contours (contourf) for more visual impact ?

import matplotlib.pyplot as plt
import numpy as np

# Create data
x = np.linspace(-2, 2, 50)
y = np.linspace(-2, 2, 50)
X, Y = np.meshgrid(x, y)
Z = X**2 - Y**2

# Create 3D plot
fig = plt.figure(figsize=(12, 5))

# Subplot 1: Contourf only
ax1 = fig.add_subplot(121, projection='3d')
contours = ax1.contourf(X, Y, Z, levels=10, cmap='coolwarm')
ax1.set_title('Filled Contours Only')

# Subplot 2: Layered contourf and surface
ax2 = fig.add_subplot(122, projection='3d')
ax2.contourf(X, Y, Z, levels=10, cmap='coolwarm', alpha=0.6)
ax2.plot_surface(X, Y, Z, cmap='plasma', alpha=0.4)
ax2.set_title('Layered Contourf and Surface')

plt.tight_layout()
plt.show()

Advanced Layering with Multiple Levels

Create more complex visualizations with specific contour levels ?

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
x = np.linspace(-4, 4, 100)
y = np.linspace(-4, 4, 100)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X**2 + Y**2)/4) * np.cos(2*X) * np.sin(2*Y)

# Create the plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')

# Add filled contours at the bottom
ax.contourf(X, Y, Z, zdir='z', offset=-0.5, levels=20, cmap='RdYlBu', alpha=0.8)

# Add contour lines at specific levels
ax.contour(X, Y, Z, levels=[-0.3, 0, 0.3], colors=['blue', 'black', 'red'], linewidths=2)

# Add semi-transparent surface
surf = ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.5)

# Customize the plot
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_title('Advanced Layered Visualization')
ax.set_zlim(-0.5, 0.5)

# Add colorbar
fig.colorbar(surf, shrink=0.5)

plt.show()

Key Parameters

Parameter Function Purpose
alpha Both Controls transparency (0-1)
levels contour/contourf Specifies contour levels
cmap Both Colormap selection
zdir contourf Projection direction
offset contourf Projection plane position

Conclusion

Layering contour and surface plots in Matplotlib creates rich 3D visualizations that highlight both specific levels and overall structure. Use transparency settings and complementary colormaps to ensure both layers are clearly visible.

Updated on: 2026-03-25T20:02:26+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements