How to create a surface plot from a greyscale image with Matplotlib?

Creating a surface plot from a grayscale image with Matplotlib allows you to visualize image data in 3D, where pixel intensities become height values. This technique is useful for analyzing image textures, elevation maps, or any 2D data that benefits from 3D visualization.

Basic Surface Plot from Grayscale Data

Here's how to create a 3D surface plot using grayscale image data ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [10, 6]
plt.rcParams["figure.autolayout"] = True

# Create sample grayscale image data (5x5 matrix)
data = np.random.rand(5, 5)

# Create coordinate meshgrid
xx, yy = np.mgrid[0:data.shape[0], 0:data.shape[1]]

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

# Create surface plot with grayscale colormap
surface = ax.plot_surface(xx, yy, data, rstride=1, cstride=1, 
                         linewidth=0, cmap='gray', alpha=0.8)

# Add labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Intensity')
ax.set_title('3D Surface Plot from Grayscale Data')

plt.show()

Using Real Image Data

For actual image processing, you can load and convert a real image to grayscale ?

import numpy as np
import matplotlib.pyplot as plt

# Create a more realistic grayscale image pattern
# Simulating a landscape-like elevation map
x = np.linspace(0, 10, 20)
y = np.linspace(0, 10, 20)
X, Y = np.meshgrid(x, y)

# Create elevation-like data
Z = np.sin(X) * np.cos(Y) * np.exp(-(X-5)**2/10 - (Y-5)**2/10)

# Normalize to 0-1 range (typical for grayscale)
Z = (Z - Z.min()) / (Z.max() - Z.min())

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

# Plot surface with enhanced grayscale colormap
surface = ax.plot_surface(X, Y, Z, cmap='gray', 
                         linewidth=0, antialiased=True, alpha=0.9)

# Customize the plot
ax.set_xlabel('X Coordinate')
ax.set_ylabel('Y Coordinate')
ax.set_zlabel('Pixel Intensity')
ax.set_title('3D Surface Plot from Grayscale Image Data')

# Add colorbar
plt.colorbar(surface, ax=ax, shrink=0.5, aspect=5)

plt.show()

Key Parameters

Parameter Description Common Values
cmap Colormap for surface 'gray', 'viridis', 'plasma'
rstride, cstride Row and column stride (sampling) 1 (all points), 2, 5
linewidth Width of surface grid lines 0 (no lines), 0.5, 1
alpha Surface transparency 0.8, 0.9, 1.0 (opaque)

Alternative Visualization Methods

You can also create wireframe plots or contour plots for different visual effects ?

import numpy as np
import matplotlib.pyplot as plt

# Create sample data
data = np.random.rand(8, 8)
xx, yy = np.mgrid[0:data.shape[0], 0:data.shape[1]]

# Create subplot with multiple visualization styles
fig = plt.figure(figsize=(15, 5))

# Surface plot
ax1 = fig.add_subplot(131, projection='3d')
ax1.plot_surface(xx, yy, data, cmap='gray', alpha=0.8)
ax1.set_title('Surface Plot')

# Wireframe plot
ax2 = fig.add_subplot(132, projection='3d')
ax2.plot_wireframe(xx, yy, data, color='black', linewidth=0.5)
ax2.set_title('Wireframe Plot')

# Contour plot (2D)
ax3 = fig.add_subplot(133)
contour = ax3.contour(xx, yy, data, colors='black', levels=10)
ax3.clabel(contour, inline=True, fontsize=8)
ax3.set_title('2D Contour Plot')

plt.tight_layout()
plt.show()

Conclusion

Use plot_surface() with cmap='gray' to create 3D visualizations of grayscale image data. The mgrid function generates coordinate meshes, while surface parameters like alpha and linewidth control the plot appearance. This technique effectively transforms 2D pixel intensities into 3D height maps for enhanced data analysis.

Updated on: 2026-03-26T00:31:27+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements