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, andZare 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 pointsY: The y-coordinates of the data pointsZ: 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 datatime_vector = 0:0.1:10; % Time vectorx_1 = sin(time_vector); % X-coordinatey_1 = cos(time_vector); % Y-coordinatez_1 = time_vector; % Z-coordinatea = figure();% Plot in 3Dplot3(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_1is defined as the sine oftime_vector,y_1as the cosine oftime_vector, andz_1as thetime_vectoritself. 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. Thex_1,y_1, andz_1vectors 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, andZare 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 datatime_vector = 0:0.1:10; % Time vectorx_1 = sin(time_vector); % X-coordinatey_1 = cos(time_vector); % Y-coordinatez_1 = time_vector; % Z-coordinatea = figure();% Plot in 3Dscatter3(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, andz_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, andZare 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 datatime_vector = 0:0.1:10; % Time vectorx_1 = sin(time_vector); % X-coordinatey_1 = cos(time_vector); % Y-coordinatez_1 = time_vector; % Z-coordinate% Create grid for surface plot[X, Y] = meshgrid(x_1, y_1); % Generate mesh grid from x and y coordinatesZ = X.^2 + Y.^2; % Generate Z values for the surface plot% Plot surfacea = figure();surf(X, Y, Z); % Surface plotxlabel('X-axis');ylabel('Y-axis');zlabel('Z-axis');title('3D Surface Plot Example');grid on;
Explanation
Line 8: The
meshgridfunction is used to generate coordinate arrays for 3D plots. It takes two vectors,x_1andy_1, representing the x and y-coordinates of points in a grid, and returns two 2D arrays,XandY, 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.^2squares each element of theXmatrix, andY.^2squares each element of theYmatrix. 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 equationZ = X.^2 + Y.^2.Line 13: It creates a surface plot using the
surffunction. It takes three input arguments:X,Y, andZ, which are the matrices generated bymeshgridand the corresponding z-coordinates calculated based on those grid points. Thesurffunction then generates a 3D surface plot using these coordinates. The surface is defined by the points in the grid created bymeshgrid, with heights determined by the values in theZmatrix.
Free Resources