How do I vary color along a 2D line?
845 views (last 30 days)
I am plotting the x,y position of a point over time. I have the data in two vectors, xpos and ypos, and I plot the path of this point using plot(xpos,ypos).
I would like to have this line change color gradually, representing the time axis ( or the index of the x, y vectors).
Is there an easy way to do this?
Matt Fig on 8 Apr 2011
This is one of the classic 'tricks' of MATLAB graphics.
x = 0:.05:2*pi;
y = sin(x);
z = zeros(size(x));
col = x; % This is the color, vary with x in this case.
Changed edgecolor to interp and linewidth to 2. This looks less ragged on my machine.
More Answers (3)
Daniel Refy on 7 Mar 2015
N=1 numel(data(:,1)) for a=1:N:numel(data(:,1))-N plot(data(a:a+N,1),data(a:a+N,2),'.','Color',[(a/numel(data(:,1))),0,0] ) hold on end
this will change color of the line from black to red
Paulo Silva on 8 Apr 2011
Something similar to this:
plot(xpos(1:10),ypos(1:10)) %first points in blue
plot(xpos(11:20),ypos(11:20),'g') %green ones
plot(xpos(21:end),ypos(21:end),'r') %final points in red
another way, the hold all function makes the plots with different color (until the list of available colors ends and after it start from the first one again)
... %more plots until the end of the indexes
You can insert the code inside a for loop
N=7; %every N points change color