Consider the following example of a three-dimensional point moving in a spiral over time:
%# data t = linspace(0,8*pi,200); x = 20*t; y = cos(t); z = sin(t); %# plot 3D line plot3(x,y,z) axis tight, grid on, view(35,40)
Now, if you want to draw a multi-color line, a naive solution would be to write a for-loop loop, drawing each small segment as a separate line, each of which has a different color. This is because an object with one line can have only one color.
It is better to use a surface graphic object:
c = 1:numel(t); %# colors h = surface([x(:), x(:)], [y(:), y(:)], [z(:), z(:)], ... [c(:), c(:)], 'EdgeColor','flat', 'FaceColor','none'); colormap( jet(numel(t)) )
Result:

source share