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

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

 Respuesta aceptada

Stephen23
Stephen23 el 27 de Feb. de 2016
Editada: Stephen23 el 28 de Feb. de 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 comentarios

Jakub Panuska
Jakub Panuska el 28 de Feb. de 2016
Editada: Jakub Panuska el 28 de Feb. de 2016
Thanks it works and also thank you for comment about how the matrices works in matlab!!!
And don't forget to preallocate the output arrays.

Iniciar sesión para comentar.

Más respuestas (1)

Adam
Adam el 26 de Feb. de 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 comentarios

Jakub Panuska
Jakub Panuska el 26 de Feb. de 2016
Editada: Jakub Panuska el 26 de Feb. de 2016
I added picture and full length of code.
So it looks like
Q=plot(Fplot(jj),tgr(jj),'color',ps{jj});
is what you want, although obviously if you have more curves than 6 they will not all plot.
Q=plot(Fplot,tgr,'color',ps{jj});
is a more efficient way of plotting your data without the need of a loop at all, but it will assign its own colours to each of the curves (taking each column of values in your arrays as representing 1 curve).
It does not work Adam:/ If I set
Q=plot(Fplot(jj),tgr(jj),'color',ps{jj});
It does not plot anything. And if I use second thing without for loop but with defined jj indices following message occurs.
Error using plot
Error in color/linetype argument
Error in freq_domain (line 125)
Q=plot(Fplot,tgr,'color',ps{jj});
Adam
Adam el 26 de Feb. de 2016
Editada: Adam el 26 de Feb. de 2016
Try something more like this. You cannot combine multiple colours with the syntax of plotting the 2d array in one go to get the right result, but you can plot all curves and then assign your desired colour to the plotted curves afterwards.
Q=plot(Fplot,tgr);
for n = 1:6
Q(n).Color = ps{n};
end
axis([0 60000 0 0.0015]);
tickValuesY = 0:0.00005:0.0015;
tickValuesX = 0:2500:plo;
set(gca,'YTick',tickValuesY);
set(gca,'XTick',tickValuesX);
set(Q,'LineWidth',5);
grid on
Something similar to the first approach I posted, plotting each curve individually with colour within the for loop should also work, but the code is too difficult to read to understand exactly what Fplot and tgr contain so my syntax obviously wasn't quite right.
Stephen23
Stephen23 el 26 de Feb. de 2016
Editada: Stephen23 el 27 de Feb. de 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 el 27 de Feb. de 2016
Yes, that's true. I've never actually used the 'ColorOrder' property. I forgot it exists!

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.

Preguntada:

el 26 de Feb. de 2016

Comentada:

el 29 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by