Azzera filtri
Azzera filtri

How can I display a line in 3D with a colour gradient?

4 visualizzazioni (ultimi 30 giorni)
Let’s say I have a set of 10 tridimensional points, represented as a 3 by 10 matrix, such as these points generated just for the sake of illustration:
n = 10;
x = linspace(1, 2, n);
y = cos(x);
z = y.^2;
points = [x' y' z']
points = 10x3
1.0000 0.5403 0.2919 1.1111 0.4437 0.1968 1.2222 0.3416 0.1167 1.3333 0.2352 0.0553 1.4444 0.1260 0.0159 1.5556 0.0152 0.0002 1.6667 -0.0957 0.0092 1.7778 -0.2055 0.0422 1.8889 -0.3128 0.0978 2.0000 -0.4161 0.1732
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I can display them as a line in 3D:
plot3(points(:,1,:), points(:,2,:), points(:,3,:));
My question is: how can I apply a color gradient to this line, so we can see in what direction does the line evolve (relative to the order of the point in the points matrix)?
I tried several methods, without success so far:
  • Calling plot3 with the c parameter (as suggested by an answer on the Internet): the c keyword is not recognised.
  • Calling colormap with a custom made map: no graph is displayed at all, even though no error is thrown.
cmap = [linspace(0, 1, n)' zeros(n, 1) zeros(n, 1)];
colormap(cmap);

Risposta accettata

Voss
Voss il 10 Mag 2024
n = 10;
x = linspace(1, 2, n);
y = cos(x);
z = y.^2;
points = [x' y' z']
points = 10x3
1.0000 0.5403 0.2919 1.1111 0.4437 0.1968 1.2222 0.3416 0.1167 1.3333 0.2352 0.0553 1.4444 0.1260 0.0159 1.5556 0.0152 0.0002 1.6667 -0.0957 0.0092 1.7778 -0.2055 0.0422 1.8889 -0.3128 0.0978 2.0000 -0.4161 0.1732
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
surf(points(:,[1 1]),points(:,[2 2]),points(:,[3 3]),[1:n; 1:n].', ...
'FaceColor','none','EdgeColor','interp','LineWidth',2)

Più risposte (0)

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by