How can I set different colors for lines from nested loop?
Mostrar comentarios más antiguos
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
Más respuestas (1)
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
el 26 de Feb. de 2016
Editada: Jakub Panuska
el 26 de Feb. de 2016
Adam
el 26 de Feb. de 2016
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).
Jakub Panuska
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.
"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
el 27 de Feb. de 2016
Yes, that's true. I've never actually used the 'ColorOrder' property. I forgot it exists!
Categorías
Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

