plot 3D curve with color-coded direction

조회 수: 8 (최근 30일)
Liang Zhan
Liang Zhan 2012년 7월 30일
hi, friends,
I have a 3xN matrix, which represents N points in one curve. I know plot3 can plot this curve easily, but I want to use color to represent the curve orientation. For example, if the curve is in x direction, the color is "red", if the curve is in y direction, the color is in "green", if the curve is in z direction, the color is in "blue“.
So the color will vary with the curve going from the beginning to the end.
I saw some example from other software, want to know whether it can be done in matlab.
Appreciate!
LZ

채택된 답변

Teja Muppirala
Teja Muppirala 2012년 7월 31일
Yes, you can do this in MATLAB. But it is easier to use a "surface" (in this case, I am calling MESH) to plot it instead of a line.
%Make a random 3d curve
P = interpft(rand(10,3),500);
P(end+1,:) = P(1,:);
% Make colors from the velocity
dP = diff(P([1:end 1],:));
C = abs(dP)/max(abs(dP(:)));
C = permute(cat(3,C,C),[1 3 2]);
% Use mesh to draw it
h = mesh([P(:,1) P(:,1)],[P(:,2) P(:,2)],[P(:,3) P(:,3)],C);
set(h,'lineWidth',3);
axis vis3d
  댓글 수: 2
Walter Roberson
Walter Roberson 2012년 7월 31일
Teja, continuous curves plotted with plot() or plot3() cannot change color as they progress.
Teja Muppirala
Teja Muppirala 2012년 7월 31일
Hi Walter, I meant that if he were to try to do it using lines, he would have to make many different line objects, one for each segment in a different color. That is not impossible, but it is kind of a hassle. That is why I agree with your suggestion below that surfaces (patches) are a more general and easier way to accomplish the same thing.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2012년 7월 31일
line() and lineseries objects such as are generated by plot() and plot3(), are each restricted to one color along the length. For this reason, some people prefer to instead use very narrow patch() objects; the edgecolor of patch objects can be controlled.
Question: if A, B, C are three consecutive points, with different "trends" between A-B and B-C, then should the line between A-B be all one color, and the line between B-C be all a different color, or do you want the color changes to shade between the points ?

Liang Zhan
Liang Zhan 2012년 7월 31일
Thanks Teja Muppirala, your code works very well in my case, appreciate! Also, thanks Walter Roberson for your input

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by