Skip to content

MATLAB - 3D Plots

Three-dimensional (3D) plots are essential for visualizing data that has three variables or for representing functions of two variables, z = f(x, y). MATLAB provides a comprehensive suite of functions for creating and manipulating 3D visualizations, from simple line plots to complex surfaces and volumetric data.

Here are the most common functions for 3D plotting:

  • Line Plots (plot3): Draws lines and points in 3D space. Ideal for visualizing trajectories or time-series data with three components.
  • Scatter Plots (scatter3): Displays individual markers in 3D space. Useful for visualizing data points that are not connected.
  • Mesh Plots (mesh): Creates a wireframe surface by connecting points on a grid. Excellent for viewing the underlying structure of a surface.
  • Surface Plots (surf): Renders a filled, colored surface. Best for creating smooth, realistic visualizations of 3D data.

The plot3(X, Y, Z) function plots coordinates in 3D space. If X, Y, and Z are vectors of the same length, plot3 draws a line connecting the points.

A helix is a classic example of a 3D curve. We can define it parametrically and plot it.

% Define the parameter 't'
t = 0:pi/50:10*pi;
% Define the parametric equations for the helix
x = sin(t);
y = cos(t);
z = t;
% Create the 3D plot
figure; % Create a new figure window
plot3(x, y, z, 'LineWidth', 2);
% Add labels and a title
title('3D Helix');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
grid on;

Expected Output: A window appears showing a blue, spiraling line (a helix) that rises along the z-axis. The plot is contained within a 3D axes box with a grid and labels for the X, Y, and Z axes.

You can customize the line style, marker, and color, just like with the 2D plot function.

% Using the same helix data from Example 1
t = 0:pi/50:10*pi;
x = sin(t); y = cos(t); z = t;
% Plot with custom style
figure;
plot3(x, y, z, '--ro', ...
'LineWidth', 1.5, ...
'MarkerSize', 4, ...
'MarkerFaceColor', 'y');
title('Customized 3D Helix');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
grid on;

Expected Output: The helix is now a dashed red line (--r) with circular markers (o) at each data point. The markers have a yellow fill color.

To plot a surface, you first need to define a grid of (x, y) points using meshgrid and then calculate the corresponding z-value for each point on the grid. The peaks function is a built-in sample function of two variables that is useful for demonstrating these plots.

% Generate a grid and data using the peaks function
[X, Y, Z] = peaks(30);
% Create two subplots to compare mesh and surf
figure;
% First subplot: mesh plot
subplot(1, 2, 1);
mesh(X, Y, Z);
title('Mesh Plot of Peaks');
xlabel('X'); ylabel('Y'); zlabel('Z');
% Second subplot: surface plot
subplot(1, 2, 2);
surf(X, Y, Z);
title('Surface Plot of Peaks');
xlabel('X'); ylabel('Y'); zlabel('Z');
% Add a colorbar to the surface plot
colorbar;

Expected Output: A figure window with two plots side-by-side. The left plot (mesh) shows a wireframe view of the ‘peaks’ surface, where you can see through the colored lines. The right plot (surf) shows the same surface but as a solid, opaque object with colored faces. A colorbar is displayed next to the surface plot.

Surface plots offer many customization options, such as controlling the colormap and edge colors.

% Generate data
[X, Y, Z] = peaks(30);
figure;
% Create a surface plot
surf(X, Y, Z);
% Customize the appearance
title('Customized Surface Plot');
xlabel('X'); ylabel('Y'); zlabel('Z');
% Use a different colormap
colormap(cool);
% Turn off the mesh lines on the surface
shading interp; % Interpolates colors for a smooth look
% Or use shading flat; for a faceted look
% Or use shading faceted; to keep the mesh lines
colorbar;

Expected Output: A surface plot is displayed using the ‘cool’ colormap (cyan to magenta). The shading interp command removes the black mesh lines, making the surface look smooth and continuous.

Use scatter3 when you have individual data points that are not part of a surface or line. You can customize the size and color of each marker to represent additional dimensions of your data.

Example 5: Scatter Plot with Variable Size and Color

Section titled “Example 5: Scatter Plot with Variable Size and Color”
% Create some random data
N = 100;
x = rand(N, 1) * 10;
y = rand(N, 1) * 10;
z = rand(N, 1) * 10;
% Define size and color data
marker_sizes = rand(N, 1) * 100 + 10; % Size is proportional to some variable
marker_colors = z; % Color is based on the z-value
figure;
scatter3(x, y, z, marker_sizes, marker_colors, 'filled');
title('3D Scatter Plot');
xlabel('X-value');
ylabel('Y-value');
zlabel('Z-value');
% Add a colorbar to show what the colors represent
cb = colorbar;
cb.Label.String = 'Z-Value Magnitude';

Expected Output: A 3D scatter plot displays 100 filled circular markers. The position of each marker is determined by x, y, and z. The size of each marker varies, and its color corresponds to its height (z-value), as indicated by the colorbar.