Is it possible to plot lines with different colors using single plot command or some other way?

I am having a hexagonal 2-D plot. Is it possible to change the colors of the edges of this hexagon? Is there any way to choose the colors of a-b, b-c, c-d etc , differently ? Thanks.
Here is a code example of the plot.
vertex = ['a','b','c','d','e','f'];
ang = [0:pi/3:2*pi];
x=cos(ang);
y=sin(ang);
plot(x,y)
for j=1:6
text(x(j),y(j),z(j),vertex(j));
end

 Accepted Answer

Muhammad, you could do something like:
vertex = ['a','b','c','d','e','f'];
ang = [0:pi/3:2*pi];
x = cos(ang);
y = sin(ang);
myOrder = [1, 0, 0 % red
1, 0, 0
1, 0, 0
1, 0, 0 % red
0, 0, 1 % blue
0, 0, 1]; % blue
set(gca,'ColorOrder',myOrder,'NextPlot','replacechildren');
hold on
for jj = 1:numel(ang)-1
plot([x(jj) x(jj+1)],[y(jj) y(jj+1)])
text(x(jj),y(jj),vertex(jj));
end

3 Comments

Thanks Mischa for your reply, its helpful. Is there any way to keep the same colors of 5 edges except the one?
Actually I am trying to use this plot in a long loop for animation purpose, and I want to keep some edges with same color but others different depending on a logical operation. I am also trying to avoid the "FOR" loop for plotting purpose. Let me tell you scenario, vertex a,b,c,d,e remain stationary but 'f' would be changing its coordinates. So the edges between a,b,c,d,e would be of same color but the edges e-f and f-a must be different during that long "FOR" loop.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!