Matplotlib - Surface Plots



A surface plot is a way to represent three-dimensional mathematical function or data on a flat, two-dimensional surface. It shows how the value of the function or data changes across two input variables (usually represented on the x and y axes) and how this change affects the output variable (represented on the z-axis).

Imagine you have a table with two columns representing two independent variables, and a third column representing a dependent variable. The surface plot takes these three columns of data and creates a 3D representation where the two independent variables are on the x and y axes, and the dependent variable is represented by the height or color of the surface −

Surface Plot

Surface Plots in Matplotlib

We can create a surface plot in Matplotlib using the plot_surface() function from the mpl_toolkits.mplot3d module. This function is useful for visualizing functions of two variables or three-dimensional datasets. It generates a surface plot that provides a more clear view of the data compared to traditional two-dimensional plots.

The plot_surface() Function

The plot_surface() function in Matplotlib accepts x and y coordinates, organized in a grid-like structure using meshgrid, and the corresponding z values determine the height or value at each grid point. The resulting plot gives you a visual representation of the 3D shape of your data.

Following is the syntax of plot_surface() function in Matplotlib −

ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='black', **kwargs)

Where,

  • X is the x-coordinates of the values (2D array or meshgrid).
  • Y is the y-coordinates of the values (2D array or meshgrid).
  • Z is the z-coordinaes of the values (2D array or meshgrid).
  • cmap is the colormap for coloring the surface. It specifies the color mapping for the surface based on the Z values.
  • edgecolor is the color of the edges of the surface.
  • **kwargs are the additional keyword arguments for customization (e.g., colors, transparency).

Saddle-shaped Surface Plot

The saddle-shaped surface is a type of three-dimensional plot in Matplotlib that visualizes a mathematical surface resembling a saddle or a hyperbolic paraboloid. This surface is defined by the mathematical function z=x2-y2. The plot provides a clear picture of how the function values change across the X and Y dimensions.

Example

In the following example, we are defining a saddle-shaped surface using the "saddle_surface" function and generating data points for the surface over a specified range. We then create a 3D surface plot using the plot_surface() function, displaying the saddle-shaped surface with a "Viridis" colormap and "black" edges −

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

# Defining a saddle-shaped surface
def saddle_surface(x, y):
    return x**2 - y**2

# Generating data points
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = saddle_surface(X, Y)

# Creating a surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='black')

ax.set_title('Saddle-shaped Surface')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

Output

After executing the above code, we get the following output −

Saddle-shaped Surface

Gaussian Surface Plot

The Gaussian surface plot in Matplotlib is a three-dimensional representation of a mathematical function known as a Gaussian distribution or bell curve. This type of surface plot provides a visual representation of the function z=e-(x2+y2), where x and y are the input variables.

Example

In here, we are defining a Gaussian surface using the function "gaussian_surface", and then generating a set of data points for this surface over a specified range. We then create a 3D surface plot displaying the Gaussian surface using the plot_surface() function −

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

# Defining a Gaussian surface
def gaussian_surface(x, y):
    return np.exp(-(x**2 + y**2))

# Generating points
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)
Z = gaussian_surface(X, Y)

# Creating a surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='plasma', edgecolor='black')

ax.set_title('Gaussian Surface')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

Output

Following is the output of the above code −

Gaussian Surface Plot

Sine and Cosine Surface Plots

The sine and cosine surface plots in Matplotlib represent a three-dimensional visualization of a mathematical function combining sine and cosine terms. These plots show the behaviour of the function z=sin(√x2+y2)+cos(√x2+y2), where x and y are the input variables

Example

Now, we are generating a 3D surface plot by combining the values of the sine and cosine functions over a meshgrid. The resulting surface is formed by adding the heights of the sine and cosine waves at each point in the grid −

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

# Creating a meshgrid
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(x, y)

# Defining a surface using sine and cosine functions
Z = np.sin(np.sqrt(X**2 + Y**2)) + np.cos(np.sqrt(X**2 + Y**2))

# Creating a surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis', edgecolor='black')

ax.set_title('Sine and Cosine Surface')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

Output

Output of the above code is as follows −

Sine and Cosine Surface Plots

Random Noise Surface Plot

The random noise surface plot in Matplotlib is a 3D representation of a surface generated with random noise. This type of plot is useful for visualizing data with an element of randomness or variability.

Example

In the example below, we are generating a 3D surface plot representing random noise. The np.random.rand() function creates a grid of random values between 0 and 1, forming the heights of the surface. The plot is displayed using Matplotlib with a "Cividis" colormap and "black" edges, showing a surface with random fluctuations in height across the specified x and y ranges −

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

# Generating random noise for a surface
np.random.seed(42)
X = np.linspace(-5, 5, 100)
Y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(X, Y)
Z = np.random.rand(100, 100)

# Creating a surface plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='cividis', edgecolor='black')

ax.set_title('Random Noise Surface')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()

Output

The output obtained is as shown below −

Random Noise Surface Plot
Advertisements