Plotting an imshow() image in 3d in Matplotlib

To plot an imshow() image in 3D in Matplotlib, you can display 2D data as both a traditional image and as a 3D surface plot. This technique is useful for visualizing data from different perspectives.

Step-by-Step Approach

Here's the process to create 3D visualizations of imshow data ?

  • Create xx and yy coordinate grids using numpy

  • Generate 2D data using mathematical functions

  • Create a figure with multiple subplots

  • Display data as a 2D image using imshow()

  • Display the same data as a 3D contour plot

  • Show the combined visualization

Basic Example

This example creates a 2D function and displays it both as an image and 3D plot ?

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

# Set figure size
plt.rcParams["figure.figsize"] = [10.0, 5.0]
plt.rcParams["figure.autolayout"] = True

# Create coordinate grids
xx, yy = np.meshgrid(np.linspace(0, 1, 50), np.linspace(0, 1, 50))

# Generate 2D data
data = np.cos(4 * np.pi * xx) * np.sin(4 * np.pi * yy)

# Create figure with subplots
fig = plt.figure()

# 2D imshow plot
ax1 = fig.add_subplot(121)
im = ax1.imshow(data, cmap="viridis", interpolation='bilinear', 
                origin='lower', extent=[0, 1, 0, 1])
ax1.set_title('2D Image View')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')

# 3D contour plot
ax2 = fig.add_subplot(122, projection='3d')
ax2.contourf(xx, yy, data, levels=50, zdir='z', offset=-1, cmap="viridis")
ax2.set_title('3D Contour View')
ax2.set_xlabel('X')
ax2.set_ylabel('Y')
ax2.set_zlabel('Z')

plt.tight_layout()
plt.show()
[Displays a side-by-side comparison of 2D imshow and 3D contour plots]

3D Surface Plot

You can also create a true 3D surface instead of contours ?

import matplotlib.pyplot as plt
import numpy as np

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

fig = plt.figure(figsize=(12, 5))

# 2D imshow
ax1 = fig.add_subplot(131)
im1 = ax1.imshow(Z, cmap='plasma', extent=[-2, 2, -2, 2])
ax1.set_title('2D Image')
plt.colorbar(im1, ax=ax1)

# 3D surface
ax2 = fig.add_subplot(132, projection='3d')
surf = ax2.plot_surface(X, Y, Z, cmap='plasma', alpha=0.8)
ax2.set_title('3D Surface')

# 3D wireframe
ax3 = fig.add_subplot(133, projection='3d')
ax3.plot_wireframe(X, Y, Z, color='black', alpha=0.6)
ax3.set_title('3D Wireframe')

plt.tight_layout()
plt.show()
[Displays three different visualizations of the same 2D data]

Key Parameters

Parameter Description Example Values
cmap Colormap for visualization 'viridis', 'plasma', 'jet'
projection='3d' Enable 3D plotting Required for 3D axes
zdir Direction for 3D projection 'x', 'y', 'z'
offset Position along zdir axis Numeric value

Conclusion

Combining imshow() with 3D plotting provides powerful visualization capabilities. Use contourf() for filled contours or plot_surface() for true 3D surfaces to display your 2D data from multiple perspectives.

Updated on: 2026-03-26T00:24:40+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements