How to interpolate color along a curve with specific colors?

48 views (last 30 days)
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 Comment
xiaojuezi
xiaojuezi on 13 Apr 2020
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);

Sign in to comment.

Accepted Answer

darova
darova on 11 Apr 2020
  • 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'

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by