0
0
Matplotlibdata~5 mins

3D surface plots in Matplotlib

Choose your learning style9 modes available
Introduction

3D surface plots help us see how two inputs relate to an output in a smooth, curved shape. They make it easy to understand complex data with three dimensions.

To visualize how temperature changes over a geographic area with height.
To explore how sales depend on price and advertising budget together.
To understand the shape of a mathematical function with two variables.
To show the relationship between time, speed, and distance in physics.
To analyze how two factors affect a result in experiments or simulations.
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')
X, Y = np.meshgrid(x_values, y_values)
Z = some_function(X, Y)
ax.plot_surface(X, Y, Z, cmap='viridis')
plt.show()

Use projection='3d' to create a 3D plot.

np.meshgrid creates coordinate grids for X and Y.

Examples
This example plots a wavy surface using sine of the distance from the center.
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

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))

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='coolwarm')
plt.show()
This example shows a saddle shape surface from a simple math formula.
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x = np.linspace(-3, 3, 30)
y = np.linspace(-3, 3, 30)
X, Y = np.meshgrid(x, y)
Z = X**2 - Y**2

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='plasma')
plt.show()
Sample Program

This program creates a smooth 3D surface shaped by a combination of exponential decay and sine/cosine waves. The color map helps show height differences.

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

# Create data points
x = np.linspace(-4, 4, 40)
y = np.linspace(-4, 4, 40)
X, Y = np.meshgrid(x, y)

# Define Z as a function of X and Y
Z = np.exp(-0.1 * (X**2 + Y**2)) * np.cos(X) * np.sin(Y)

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

# Plot the surface with color map
surf = ax.plot_surface(X, Y, Z, cmap='viridis')

# Add a color bar to show the scale
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()
OutputSuccess
Important Notes

3D plots can be rotated interactively in the plot window to see different angles.

Choosing a good color map helps make the surface easier to understand.

Meshgrid size affects plot detail and performance; bigger grids show smoother surfaces but take longer.

Summary

3D surface plots show how two inputs affect one output in a smooth shape.

Use projection='3d' and plot_surface in matplotlib to create them.

Meshgrid creates the grid of points needed for the surface.