How can I set different colors for lines from nested loop?

9 views (last 30 days)
Hi I found this scheme here in forum and it is often questioned but in my case it does not work. Can someone help me to understand why?
I made it simpler. Here is the code. Each line should have color defined in ps depend on number of line from 1 to 6. All of the lines have color magenta (last value in ps).
x=1:10;
qqq=1;
for j=0:5;
b=j;
Fplot(:,qqq)=x+b;
qqq=qqq+1;
hold on
ps={'k','b','r','g','y','m'};
for i=1:6;
Q=plot(x,Fplot,'color',ps{i});
end
end
hold off

Accepted Answer

Stephen23
Stephen23 on 27 Feb 2016
Edited: Stephen23 on 28 Feb 2016
This is the figure that your code generates:
"All of the lines have color magenta (last value in ps)"
No they do not. You plot multiple lines in the same location and the top line is always magenta, and covers all of the other lines up. Your plot does not have 6 magenta lines on it, as you think, it actually has 126 lines using every color that you have plotted with:
>> C = get(gca,'Children');
>> numel(C)
ans =
126
>> all(strcmpi(get(C,'Type'),'line'))
ans =
1
>> M = cell2mat(get(C,'Color')); % all RGB colors
>> unique(M,'rows') % RGB colors used
ans =
0 0 0
0 0 1
0 1 0
1 0 0
1 0 1
1 1 0
You see, every color that you used is still there, but you covered them up plotting magenta lines over them. "Each line should have color defined in ps": I just showed you that they do!
The problem is not MATLAB, the problem is that you expect to see lines that are underneath other lines. It seems that you really just want to plot the matrix Fplot, but you are making everything way too complicated by plotting inside the loop. By doing this you have plotted the matrix while you are still calculating its values. Just move the plot command outside the loop and plot the entire matrix once all the values have been calculated:
x=1:10;
qqq=1;
for j=0:5;
b=j;
Fplot(:,qqq)=x+b;
qqq=qqq+1;
end
plot(x,Fplot)
which generates this figure:
If you want to change the colors then I would recommend that you use the ColorOrder property, exactly as I explained in my earlier comment:
axes('ColorOrder',cool(size(Fplot,2)), 'NextPlot','replacechildren')
plot(x,Fplot)
  2 Comments
Jakub Panuska
Jakub Panuska on 28 Feb 2016
Edited: Jakub Panuska on 28 Feb 2016
Thanks it works and also thank you for comment about how the matrices works in matlab!!!

Sign in to comment.

More Answers (1)

Adam
Adam on 26 Feb 2016
Q=plot(Fplot,tgr,'color',ps{jj});
Shouldn't this line be getting its plot data from somewhere different each time round the loop?
It may plot all curves at once without needing the loop, if the arrays are of the right orientation for your x and y data, but for your code to apply colour it will plot the same lines every loop and the final instruction is to plot them in magenta.
I assume you need something like, for example
Q=plot(Fplot,tgr(jj),'color',ps{jj});
but obviously that is just a guess without knowing the structure of your data arrays.
That would plot different values on the y axis each time round the loop.
  6 Comments
Stephen23
Stephen23 on 26 Feb 2016
Edited: Stephen23 on 27 Feb 2016
"You cannot combine multiple colours with the syntax of plotting the 2d array in one go to get the right result"
Oh yes you can, by simply setting the ColorOrder property. Here is an example (taken from my FEX submission ColorBrewer colormaps brewermap):
N = 6;
axes('ColorOrder',brewermap(N,'Pastel2'), 'NextPlot','replacechildren')
X = linspace(0,pi*3,1000);
Y = bsxfun(@(x,n)n*sin(x+2*n*pi/N), X.', 1:N);
plot(X,Y, 'linewidth',4)
Using the ColorOrder to set the plot colors is so simple: it is a shame that beginners fight with loops just because they think this is the only way to get different colors for each line.
Adam
Adam on 27 Feb 2016
Yes, that's true. I've never actually used the 'ColorOrder' property. I forgot it exists!

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!