MATLAB - 3D Plots
MATLAB - 3D Plots
Section titled “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.
Common Types of 3D Plots
Section titled “Common Types of 3D Plots”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.
1. 3D Line Plots with plot3
Section titled “1. 3D Line Plots with plot3”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.
Example 1: Plotting a Helix
Section titled “Example 1: Plotting a Helix”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 helixx = sin(t);y = cos(t);z = t;
% Create the 3D plotfigure; % Create a new figure windowplot3(x, y, z, 'LineWidth', 2);
% Add labels and a titletitle('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.
Example 2: Customizing plot3
Section titled “Example 2: Customizing plot3”You can customize the line style, marker, and color, just like with the 2D plot function.
% Using the same helix data from Example 1t = 0:pi/50:10*pi;x = sin(t); y = cos(t); z = t;
% Plot with custom stylefigure;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.
2. Surface and Mesh Plots
Section titled “2. Surface and Mesh Plots”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.
Example 3: mesh vs. surf
Section titled “Example 3: mesh vs. surf”% Generate a grid and data using the peaks function[X, Y, Z] = peaks(30);
% Create two subplots to compare mesh and surffigure;
% First subplot: mesh plotsubplot(1, 2, 1);mesh(X, Y, Z);title('Mesh Plot of Peaks');xlabel('X'); ylabel('Y'); zlabel('Z');
% Second subplot: surface plotsubplot(1, 2, 2);surf(X, Y, Z);title('Surface Plot of Peaks');xlabel('X'); ylabel('Y'); zlabel('Z');
% Add a colorbar to the surface plotcolorbar;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.
Example 4: Customizing Surface Appearance
Section titled “Example 4: Customizing Surface Appearance”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 plotsurf(X, Y, Z);
% Customize the appearancetitle('Customized Surface Plot');xlabel('X'); ylabel('Y'); zlabel('Z');
% Use a different colormapcolormap(cool);
% Turn off the mesh lines on the surfaceshading 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.
3. 3D Scatter Plots with scatter3
Section titled “3. 3D Scatter Plots with scatter3”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 dataN = 100;x = rand(N, 1) * 10;y = rand(N, 1) * 10;z = rand(N, 1) * 10;
% Define size and color datamarker_sizes = rand(N, 1) * 100 + 10; % Size is proportional to some variablemarker_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 representcb = 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.