How to interpolate color along a curve with specific colors?

조회 수: 46 (최근 30일)
xiaojuezi
xiaojuezi 2020년 4월 11일
댓글: xiaojuezi 2020년 4월 13일
Hi, I have a curve of dimension 50x2, and a specification of the colors at certain positions. For example, at position 1 the color is [1,0,0] and at position 30, the color is [0,1,0] and at position 50 the color is [0,0,1]. I would like to plot this curve such that, at positions 1, 30 and 50, they have the specified colors, while at other positions, the colors are interpolated. And optimally I would like to have continuous plot rather than dividing the curve into segments.
I read from this trick that you can plot it as a surface with no face color, but I have no idea how would you specify the colors with this method. In th trick they have:
x = 0:.05:2*pi;
y = sin(x);
z = zeros(size(x));
col = x; % This is the color, vary with x in this case.
surface([x;x],[y;y],[z;z],[col;col],...
'facecol','no',...
'edgecol','interp',...
'linew',2);
I don't understand why they need [x;x] instead of just x, y and z, but removing the additional x the trick fails. Does anyone have some ideas on solving this problem?
Thank you very much!
  댓글 수: 1
xiaojuezi
xiaojuezi 2020년 4월 13일
For anyone interested, the color map in this case should be set as:
c(:,1:30,1) = ones(1,30).*linspace(1,0,30);
c(:,1:30,2) = ones(1,30).*linspace(0,1,30);
c(:,1:30,3) = ones(1,30).*linspace(0,0,30);
c(:,31:50,1) = ones(1,20).*linspace(0,0,20);
c(:,31:50,2) = ones(1,20).*linspace(1,0,20);
c(:,31:50,3) = ones(1,20).*linspace(0,1,20);

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

채택된 답변

darova
darova 2020년 4월 11일
  • I don't understand why they need [x;x] instead of just x, y and z, but removing the additional x the trick fails. Does anyone have some ideas on solving this problem?
Try this trick
Because it's actualy a surface. And surface wants it's inputs to be always of 2x2 size minimum
Here is another way to plot color line
x = 0:10;
y = sin(x);
c = jet(length(x)+1); % can be array of size Nx3 or Nx1
cla
patch('xdata',[x nan],... % add NaN to prevent MATLAB to draw faces
'ydata',[y nan],...
'facevertexcdata',c,...
'edgecolor','flat') % or 'interp'

추가 답변 (0개)

카테고리

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