3D Line Plots in MATLAB®
How to make 3D Line Plots in MATLAB® with Plotly.
Plot 3-D Helix
Define t
as a vector of values between 0 and 10π. Define st
and ct
as vectors of sine and cosine values. Then plot st
, ct
, and t
.
t = 0:pi/50:10*pi;
st = sin(t);
ct = cos(t);
plot3(st,ct,t)
fig2plotly(gcf);
Plot Multiple Lines
Create two sets of x-, y-, and z-coordinates.
t = 0:pi/500:pi;
xt1 = sin(t).*cos(10*t);
yt1 = sin(t).*sin(10*t);
zt1 = cos(t);
xt2 = sin(t).*cos(12*t);
yt2 = sin(t).*sin(12*t);
zt2 = cos(t);
Call the plot3
function, and specify consecutive XYZ
triplets.
t = 0:pi/500:pi;
xt1 = sin(t).*cos(10*t);
yt1 = sin(t).*sin(10*t);
zt1 = cos(t);
xt2 = sin(t).*cos(12*t);
yt2 = sin(t).*sin(12*t);
zt2 = cos(t);
plot3(xt1,yt1,zt1,xt2,yt2,zt2)
fig2plotly(gcf);
Plot Multiple Lines Using Matrices
Create matrix X
containing three rows of x-coordinates. Create matrix Y
containing three rows of y-coordinates.
t = 0:pi/500:pi;
X(1,:) = sin(t).*cos(10*t);
X(2,:) = sin(t).*cos(12*t);
X(3,:) = sin(t).*cos(20*t);
Y(1,:) = sin(t).*sin(10*t);
Y(2,:) = sin(t).*sin(12*t);
Y(3,:) = sin(t).*sin(20*t);
Create matrix Z
containing the z-coordinates for all three sets.
t = 0:pi/500:pi;
X(1,:) = sin(t).*cos(10*t);
X(2,:) = sin(t).*cos(12*t);
X(3,:) = sin(t).*cos(20*t);
Y(1,:) = sin(t).*sin(10*t);
Y(2,:) = sin(t).*sin(12*t);
Y(3,:) = sin(t).*sin(20*t);
Z = cos(t);
Plot all three sets of coordinates on the same set of axes.
t = 0:pi/500:pi;
X(1,:) = sin(t).*cos(10*t);
X(2,:) = sin(t).*cos(12*t);
X(3,:) = sin(t).*cos(20*t);
Y(1,:) = sin(t).*sin(10*t);
Y(2,:) = sin(t).*sin(12*t);
Y(3,:) = sin(t).*sin(20*t);
Z = cos(t);
plot3(X,Y,Z)
fig2plotly(gcf);
Specify Equally-Spaced Tick Units and Axis Labels
Create vectors xt
, yt
, and zt
.
t = 0:pi/500:40*pi;
xt = (3 + cos(sqrt(32)*t)).*cos(t);
yt = sin(sqrt(32) * t);
zt = (3 + cos(sqrt(32)*t)).*sin(t);
Plot the data, and use the axis equal
command to space the tick units equally along each axis. Then specify the labels for each axis.
t = 0:pi/500:40*pi;
xt = (3 + cos(sqrt(32)*t)).*cos(t);
yt = sin(sqrt(32) * t);
zt = (3 + cos(sqrt(32)*t)).*sin(t);
plot3(xt,yt,zt)
axis equal
xlabel('x(t)')
ylabel('y(t)')
zlabel('z(t)')
fig2plotly(gcf);