MATLAB Answers

How do I vary color along a 2D line?

745 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?

  1 Comment

Rajiv
Rajiv on 10 Feb 2014
Can be show line plot with different color, if not to use the surface plot as Matt did.

Sign in to comment.

Accepted Answer

Matt Fig
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.
surface([x;x],[y;y],[z;z],[col;col],...
'facecol','no',...
'edgecol','interp',...
'linew',2);
EDIT
Changed edgecolor to interp and linewidth to 2. This looks less ragged on my machine.

  9 Comments

Show 6 older comments
Jasmeet Singh
Jasmeet Singh on 10 Aug 2016
The above method is giving the following warning for me:
Warning: Error creating or updating Surface Error in value of property ZData Array is wrong shape or size
My x values are real positive numbers but y values include both -Inf and NaN. Do I have to get rid of these non-finite values before plotting or the problem is something else?
Jason Fevang
Jason Fevang on 21 Mar 2017
I had this problem, and I had to transpose my vectors before it worked
Dorian Hargarten
Dorian Hargarten on 12 Jul 2018
@Loginatorist, this is an awesome trick, thank you very much!

Sign in to comment.

More Answers (2)

Daniel Refy
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

  1 Comment

Ariel Szklanny
Ariel Szklanny on 2 Dec 2018
This worked great for me, thanks!

Sign in to comment.


Paulo Silva
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)
clf
hold all
plot(xpos(1:10),ypos(1:10))
plot(xpos(11:20),ypos(11:20))
plot(xpos(21:31),ypos(21:31))
... %more plots until the end of the indexes
You can insert the code inside a for loop
clf
hold all
N=7; %every N points change color
for a=1:N:numel(xpos)-N
plot(xpos(a:a+N),ypos(a:a+N))
end

  2 Comments

Kathryn McCormick
Kathryn McCormick on 8 Apr 2011
Thanks for the reply. I want to vary the color continuously, not in chunks.
Paulo Silva
Paulo Silva on 8 Apr 2011
You can change the order of the colors and give the impression of it being continously.
axes
MyColorOrder=get(gca,'ColorOrder'); %RGB combinations per row
%make your own color order, you can even add more row (more possible colors)
set(gca,'ColorOrder',MyColorOrder); %RGB combinations per row
hold all
N=7; %every N points change color
for a=1:N:numel(xpos)-N
plot(xpos(a:a+N),ypos(a:a+N))
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!