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'
  2 件のコメント
xiaojuezi
xiaojuezi 2020 年 4 月 13 日
Thank you very much!
darova
darova 2020 年 4 月 13 日
you are welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by