Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Plot: different lines colours

Subject: Plot: different lines colours

From: Lorenzo

Date: 20 Jan, 2009 04:02:31

Message: 1 of 8

Hi,
I have a matrix A nxm and I need to plot every column of A inside the same figure and I need matlab to use a different colour every time it plots a column. I'm using a for cycle. If b is a vector of length n, what I wanna do is:

for i=1:m
   plot(b,A(:,i))
   hold on
end

and I want a different line colour for every i.
How can i do?

Thanx

Lorenzo

Subject: Plot: different lines colours

From: Matt Fig

Date: 20 Jan, 2009 04:22:02

Message: 2 of 8

x = 0:.01:10;
A(:,1) = sin(x);
A(:,2) = cos(x);
A(:,3) = cos(x).*sin(x);
plot(A)




'khchloob:et aaE.e a voa aYbeal M@t h uotua'ygpimom ipnecno

Subject: Plot: different lines colours

From: Matt Fig

Date: 20 Jan, 2009 04:34:01

Message: 3 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <gl3jha$f5b$1@fred.mathworks.com>...
> x = 0:.01:10;
> A(:,1) = sin(x);
> A(:,2) = cos(x);
> A(:,3) = cos(x).*sin(x);
> plot(A)
>
>
>
>
> 'khchloob:et aaE.e a voa aYbeal M@t h uotua'ygpimom ipnecno


Or even more natural (!):

plot(x,A)


You get the point either way...




'xd93mibxbmxngaZZ^Zxn\rZ^ffmhahex>x ^hRia [x\oxZ^`ZehZh[Fhg

Subject: Plot: different lines colours

From: Pekka Kumpulainen

Date: 20 Jan, 2009 08:03:02

Message: 4 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <gl3k7p$3r0$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <gl3jha$f5b$1@fred.mathworks.com>...
> > x = 0:.01:10;
> > A(:,1) = sin(x);
> > A(:,2) = cos(x);
> > A(:,3) = cos(x).*sin(x);
> > plot(A)
> >
> >
> >
> >
> > 'khchloob:et aaE.e a voa aYbeal M@t h uotua'ygpimom ipnecno
>
>
> Or even more natural (!):
>
> plot(x,A)
>
>
> You get the point either way...
>
>
>
>
> 'xd93mibxbmxngaZZ^Zxn\rZ^ffmhahex>x ^hRia [x\oxZ^`ZehZh[Fhg

A hint for future needs:
The colours used by plot are controlled by the ColorOrder property of the axes object.

Subject: Plot: different lines colours

From: Lorenzo

Date: 20 Jan, 2009 09:36:34

Message: 5 of 8

Ok, thank you all!

Subject: Plot: different lines colours

From: us

Date: 20 Jan, 2009 09:47:01

Message: 6 of 8

Lorenzo
> I have a matrix A nxm and I need to plot every column of A inside the same figure and I need matlab to use a different colour every time it plots a column...

others have shown you the typical solution...
ML's extensive graphics handle capability offers (yet) another one

% the data
     n=5;
     m=10;
     x=1:5;
     y=reshape(1:m*n,m,n);
% the engine
     cmap=num2cell(hot(m),2); % <- use your color map/spec...
     lh=line(x,y,'marker','s');
     set(lh,...
          {'markerfacecolor'},cmap,...
          {'color'},cmap);

us

Subject: Plot: different lines colours

From: Lorenzo

Date: 20 Jan, 2009 11:22:31

Message: 7 of 8

Just another question please:
How can I automatically generate a legend?
For example, if I want to name every plot with a number i=1:n, how can I do?

Subject: Plot: different lines colours

From: Paul Mennen

Date: 21 Jan, 2009 08:14:02

Message: 8 of 8

Lorenzo <liquidcrystal@tiscali.it> wrote
> Just another question please:
> How can I automatically generate a legend?
> For example, if I want to name every plot with a number i=1:n, how can I do?

Since it seems that you are plotting lots of traces, I have a suggestion for you. Instead of "plot(x,A)" as suggested above, try "plt(x,A)". (Search for "plt" on the file exchange to download it).

One advantage relates to your legend question. plt always automatically generates a legend when plotting more than one trace. If you specify trace names (using the 'TraceID' parameter), it will use those trace names in the legend. Otherwise it will just use Line01, Line02, ...

A major advance over the standard Matlab legends though is that you can use plt's legend to enable/disable individual traces. Just click on one of the legend entries, and the associated trace will toggle on or off. (There are shortcuts for turning them all on or off). The more traces you have, the more valuable you will find this feature.

And relating to your trace color question, plt uses a much longer sequence by default so that in fact it won't repeat a color until the 100th trace! Of course you can change the default color selections for the traces as well as for everything else (axes, backgrounds, grid lines, cursor controls, etc).

You can also use plt as an alternative to plotyy, plotyyy, etc. And plt's interface isn't nearly so mysterious as with those functions. It's all spelled out clearly in the plt.chm and plt.htm help files along with dozens of example m files.

Another advantage, is that the author of plt is always willing to discuss it with you if you find a problem, something that doesn't work as you expect, or something you wish it did. I know, because that person happens to be me :)

Just try me at: paul (at) mennen (dot) org.

~Paul Mennen

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us