How to create a 3D plot in MATLAB

MATLAB allows us to create multidimensional plots. 2D plots are common and can be drawn using plot() and scatter(). We can create 3D plots using plot3() or other functions designed for 3D visualization, such as scatter3() or surf()

The plot3() function

To create a 3D plot in MATLAB, we can use the plot3() function to visualize three-dimensional data points. This allows for data representation in a spatial context, providing insights into relationships between variables that may not be as apparent in traditional 2D plots.

Syntax

The basic syntax of the plot3() function is as follows:

plot3(X, Y, Z)

Parameters

In the syntax above:

  • X, Y, and Z are vectors or matrices representing the coordinates of the data points in 3D space.

Here's a breakdown of what each argument represents:

  • X: The x-coordinates of the data points

  • Y: The y-coordinates of the data points

  • Z: The z-coordinates of the data points

Implementation of the plot3() function

Let’s review an example of creating a 3D plot using the plot3() function in the code below:

% Define some data
time_vector = 0:0.1:10; % Time vector
x_1 = sin(time_vector); % X-coordinate
y_1 = cos(time_vector); % Y-coordinate
z_1 = time_vector; % Z-coordinate
a = figure();
% Plot in 3D
plot3(x_1, y_1, z_1, 'LineWidth', 2);
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Plot Example');
grid on;

Explanation

  • Line 2: It creates a time vector ranging from 0 to 10 with a step size of 0.1. It is created using the colon operator :.

  • Lines 3–4: Here, x_1 is defined as the sine of time_vector, y_1 as the cosine of time_vector, and z_1 as the time_vector itself. These vectors will serve as coordinates for plotting in 3D space.

  • Line 7: It creates a new figure window and assigns it to the variable a.

  • Line 9: The plot3() function is used to plot the 3D data points. The x_1, y_1, and z_1 vectors are passed as input arguments. Additionally, the 'LineWidth' property is set to 2 to specify the thickness of the line connecting the data points.

  • Lines 10–12: Here, we label the x-axis, y-axis, and z-axis respectively.

  • Lines 13–14: Here, we add a title to the plot, and then add a grid to the plot for better visualization.

The scatter3() function

The scatter3() function is used to create 3D scatter plots. This function allows us to visualize data points in three-dimensional space.

Syntax

The basic syntax of the scatter3() function is as follows:

scatter3(X, Y, Z)

Parameters

  • In the syntax above, X, Y, and Z are vectors or matrices representing the coordinates of the data points in 3D space.

Implementation of the scatter3() function

Let’s review an example of creating a 3D plot using the scatter3() function in the code below:

% Define some data
time_vector = 0:0.1:10; % Time vector
x_1 = sin(time_vector); % X-coordinate
y_1 = cos(time_vector); % Y-coordinate
z_1 = time_vector; % Z-coordinate
a = figure();
% Plot in 3D
scatter3(x_1, y_1, z_1, 'filled');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Scatter Plot Example');
grid on;

Explanation

  • Line 9: It creates a 3D scatter plot with data points defined by the vectors x_1, y_1, and z_1, filling the markers to make them visually distinct.

The surf() function

We can use the surf() function in MATLAB to generate 3D surface plots, visualizing data over a grid in three dimensions.

Syntax

The basic syntax of the surf() function is as follows:

surf(X, Y, Z)

Parameters

  • In the syntax above, X, Y, and Z are vectors or matrices representing the coordinates of the data points in 3D space.

Implementation of the surf() function

Let’s review an example of creating a 3D plot using the surf() function in the code below:

% Define some data
time_vector = 0:0.1:10; % Time vector
x_1 = sin(time_vector); % X-coordinate
y_1 = cos(time_vector); % Y-coordinate
z_1 = time_vector; % Z-coordinate
% Create grid for surface plot
[X, Y] = meshgrid(x_1, y_1); % Generate mesh grid from x and y coordinates
Z = X.^2 + Y.^2; % Generate Z values for the surface plot
% Plot surface
a = figure();
surf(X, Y, Z); % Surface plot
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Surface Plot Example');
grid on;

Explanation

  • Line 8: The meshgrid function is used to generate coordinate arrays for 3D plots. It takes two vectors, x_1 and y_1, representing the x and y-coordinates of points in a grid, and returns two 2D arrays, X and Y, representing the x and y-coordinates of each point in the grid.

  • Line 9: Here, we use element-wise operations in MATLAB. The .^ operator denotes element-wise exponentiation. So, X.^2 squares each element of the X matrix, and Y.^2 squares each element of the Y matrix. Then, we add these squared values together element-wise to generate the corresponding z-coordinates for each point in the grid. This creates a surface where the height at each point is determined by the equation Z = X.^2 + Y.^2.

  • Line 13: It creates a surface plot using the surf function. It takes three input arguments: X, Y, and Z, which are the matrices generated by meshgrid and the corresponding z-coordinates calculated based on those grid points. The surf function then generates a 3D surface plot using these coordinates. The surface is defined by the points in the grid created by meshgrid, with heights determined by the values in the Z matrix.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved