# How do i change color in a 3d animated plot over time?

8 views (last 30 days)
Hamza Yusuf on 12 Jan 2022
Edited: DGM on 13 Jan 2022
I have this code that animates a 3d plot with the frequency of 100 hz. I want to change the color of it but the current method I have now changes the color of the plot of the whole time and not just at that specific instance.
Here is the code:
t = 0:0.1:60;
x = 20*t; y = cos(t); z = sin(t);
view(3);
color=jet(size(t,2));
close all
for i=1:size(t,2)
plot3(x(1:1:i),y(1:1:i),z(1:1:i),'color',color(i,:))
axis([0 1800.7 -1.7 1.7 -1.3 1.3])
grid on
pause(0.01) And above is the finished plot. I wanted to change the color so the whole spectrum could have been seen and not just the color red. And it has to change while being animated in the plot.

DGM on 13 Jan 2022
Edited: DGM on 13 Jan 2022
Use hold and only draw one segment at a time. Note that for N points, there are N-1 segments, hence the length of the color table.
t = 0:0.1:60;
x = 20*t; y = cos(t); z = sin(t);
view(3);
hold on; grid on;
color = jet(size(t,2)-1);
for k = 2:size(t,2)
plot3(x(k-1:k),y(k-1:k),z(k-1:k),'color',color(k-1,:))
axis([0 1800.7 -1.7 1.7 -1.3 1.3])
pause(0.01)
end R2020b

### Community Treasure Hunt

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

Start Hunting!