Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot3 line color based on value

I have a set of data that contains 3d Cartesian points (x, y, z) and a time stamp.

I would like to plot this data as a connected line in 3d space with the line colour changing based on the time stamp value.

Effectively i want to show the time difference in a colorbar.

Does anyone know of a way to do this?

like image 802
Fantastic Mr Fox Avatar asked Aug 07 '12 22:08

Fantastic Mr Fox


1 Answers

Consider the following example of a 3D point moving along a helix-shaped path 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-colored line, the naive solution would be to write a for-loop, drawing each small segment as a separate line, each having a different color. This is because a single line object can only have one color.

A better approach is to use a surface graphics object:

c = 1:numel(t);      %# colors
h = surface([x(:), x(:)], [y(:), y(:)], [z(:), z(:)], ...
    [c(:), c(:)], 'EdgeColor','flat', 'FaceColor','none');
colormap( jet(numel(t)) )

The result:

screenshot

like image 102
Amro Avatar answered Sep 28 '22 15:09

Amro