0
0
Matplotlibdata~5 mins

3D wireframe plots in Matplotlib

Choose your learning style9 modes available
Introduction

3D wireframe plots help you see the shape of data in three dimensions. They show the structure using lines, making it easy to understand surfaces and patterns.

You want to visualize how two variables affect a third one in 3D space.
You need to explore the shape of a mathematical function with two inputs.
You want to compare surfaces or trends in three dimensions.
You are analyzing terrain or elevation data.
You want a simple 3D plot without filled surfaces to focus on structure.
Syntax
Matplotlib
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1)
plt.show()

X, Y, and Z are 2D arrays of the same shape representing coordinates.

rstride and cstride control the row and column step size for drawing lines.

Examples
Basic wireframe plot with default stride values.
Matplotlib
ax.plot_wireframe(X, Y, Z)
Wireframe with fewer lines by skipping rows and columns every 5 steps.
Matplotlib
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)
Wireframe plot with red lines.
Matplotlib
ax.plot_wireframe(X, Y, Z, color='red')
Sample Program

This program creates a 3D wireframe plot of a wave pattern using sine of the distance from the origin. It shows how the surface changes in 3D.

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

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

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

# Plot wireframe
ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2, color='blue')

# Set labels
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.show()
OutputSuccess
Important Notes

Wireframe plots are good for seeing the shape but do not fill the surface with color.

Use rstride and cstride to reduce plot complexity for large data.

Make sure X, Y, and Z have the same shape to avoid errors.

Summary

3D wireframe plots show surfaces using lines in three dimensions.

They help visualize relationships between two inputs and one output.

Adjust stride and color to customize the plot appearance.