0
0
MATLABdata~5 mins

plot3 for 3D lines in MATLAB

Choose your learning style9 modes available
Introduction

We use plot3 to draw lines in three dimensions. It helps us see data that has depth, width, and height all at once.

When you want to show the path of a flying drone in 3D space.
To visualize the shape of a roller coaster track.
When plotting the movement of a point in 3D over time.
To display 3D curves like spirals or waves.
When comparing multiple 3D lines in one graph.
Syntax
MATLAB
plot3(X, Y, Z)
plot3(X, Y, Z, LineSpec)
plot3(X1, Y1, Z1, LineSpec1, X2, Y2, Z2, LineSpec2, ...)

X, Y, and Z are vectors or matrices of the same size representing coordinates.

LineSpec controls line style, marker, and color (like '-r' for red line).

Examples
Plots a diagonal line from (1,1,1) to (5,5,5).
MATLAB
x = 1:5;
y = x;
z = x;
plot3(x, y, z)
Plots a blue 3D spiral line.
MATLAB
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
z = t;
plot3(x, y, z, '-b')
Plots two 3D lines: one red and one green.
MATLAB
x1 = [0 1 2]; y1 = [0 1 0]; z1 = [0 0 1];
x2 = [2 3 4]; y2 = [0 1 0]; z2 = [1 1 2];
plot3(x1, y1, z1, '-r', x2, y2, z2, '-g')
Sample Program

This code draws a smooth magenta spiral line in 3D space with labels and grid for clarity.

MATLAB
t = linspace(0, 4*pi, 200);
x = cos(t);
y = sin(t);
z = t/2;
plot3(x, y, z, '-m', 'LineWidth', 2)
grid on
title('3D Spiral Line')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z axis')
OutputSuccess
Important Notes

Always make sure X, Y, and Z have the same size.

Use grid on to better see the 3D structure.

You can add labels and titles to explain your plot clearly.

Summary

plot3 draws lines in 3D using X, Y, Z coordinates.

You can customize line color and style with LineSpec.

It helps visualize data with three dimensions clearly.