How to assign gradual color to a 3d line based on values of another vector

140 views (last 30 days)
Hi all, I want to assign colour to lines plotted between 3dpoints, based on the values of a fourth vector. The values of the fourth vector are not sorted in any way, and they are both positive and negative values. I did a for loop to plot a line between the nodes that have connectivity, and I'm assigning the color as well. I think I'm having problems with the way I'm sorting the fourth value. But can't figure it out how to make it work. Here is the code:
myColourMap=jet(m);
s=stresses;
[s,I]=sort(s);
for i=1:m
Intensity = I(i);
LineColour=myColourMap(Intensity,:);
plot3([ x(t1(i)) x(t2(i))], [ y(t1(i)) y(t2(i))], [ z(t1(i)) z(t2(i))],'Color', LineColour)
hold on
end
axis equal
colorbar
The four vector is the 's' vector, and when I plotted the network of lines, the colour it assign to each one, does not represent the ascending or descending of the values in vector s(which represent stresses). I appreciate very much any help of how to plot the lines based on the values of a vector, or if the problem is the sorting, then, how to obtain the ordering indices of the vector s.
Thanks a lot in advance
Martha

Accepted Answer

Mike Garrity
Mike Garrity on 13 Nov 2015
Unfortunately the line object that plot and plot3 create can't do color data interpolation. Several of the other objects can. The one that is most commonly used for this is patch. Probably because patch can do just about anything.
There's one thing to look out for when using patch for this. By default, it makes a closed loop like this:
t = linspace(0,2*pi,100);
x = cos(t);
y = sin(t);
z = t;
c = cos(t).^2;
colormap(hsv)
patch(x,y,z,c,'FaceColor','none','EdgeColor','interp')
colorbar
view(3)
You need to add a nan to tell it not to do that.
t = linspace(0,2*pi,100);
x = cos(t);
y = sin(t);
z = t;
c = cos(t).^2;
colormap(hsv)
patch([x nan],[y nan],[z nan],[c nan],'FaceColor','none','EdgeColor','interp')
colorbar
view(3)
  5 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!