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:
I have n graphs and I want different color

Subject: I have n graphs and I want different color

From: Nasser M. Abbasi

Date: 9 Nov, 2012 09:39:49

Message: 1 of 6

On 11/9/2012 3:28 AM, Debora wrote:
> Il giorno venerd 9 novembre 2012 09:31:57 UTC+1, Debora ha scritto:
>> hello,
>>
>> how do I give each graph a different color? if I have m graphics how do I
>plot each graph in a different color?
>

> sorry,
> i have n graphs and i want that each of them is of a different colour. because
>i don't know the number of my graphs, how can i attribute to them a different colour?
>

What do you mean by "graphs" ?? You mean plot?

Any way. plot() supports a color argument. If the plot
command contains more than one set of X,Y, then matlab
will automatically generate different colors for each plot
for you.

-------------------
  x=-pi:pi/10:pi;
   plot(x,sin(x),x,cos(x),x,sin(x).*cos(x))
-------------------

colors are automatic. If you want to use legend, then just
add the legend command and Matlab will match first plot
color with first name in the legend

legend('sin(x)','cos(x)','sin(x)cos(x)')

If this still does not answer your question, then may be you
can give an example.

--Nasser

Subject: I have n graphs and I want different color

From: Nasser M. Abbasi

Date: 9 Nov, 2012 10:29:24

Message: 2 of 6

On 11/9/2012 4:16 AM, Debora wrote:

> [n,m]=size(AV);
> figure;
> for i=1:m
> subplot(m,2,i)
> plot(AV(:,i))
> title('EMG')
> end
>

make a list of color letters of the same size as 'm'
something like:

my_colors = {'r','y','k'}
for i=1:m
     subplot(...,my_colors{i})
end

Subject: I have n graphs and I want different color

From: Nasser M. Abbasi

Date: 9 Nov, 2012 10:42:57

Message: 3 of 6

On 11/9/2012 4:29 AM, Nasser M. Abbasi wrote:
> On 11/9/2012 4:16 AM, Debora wrote:
>
>> [n,m]=size(AV);
>> figure;
>> for i=1:m
>> subplot(m,2,i)
>> plot(AV(:,i))
>> title('EMG')
>> end
>>
>

> make a list of color letters of the same size as 'm'
> something like:
>
> my_colors = {'r','y','k'}
> for i=1:m
> subplot(...,my_colors{i})
> end
>

Another option, if you do not want to specify the list
of colors like the above, is to you rgb colors, and use
the country to encode the color values.

something like

x=-pi:pi/10:pi;
for i=1:10
   plot(x,foo(i),'Color',[1,1/i,0.6])
end

This way, you can generate the color coding on the fly for
as many plots. You need to determine the scaling for
the rgb colors based on the current index of the loop.
(since the are from 0 to 1 each). (use the maximum number
of plots to map to '1' for example.)

This is left as an exercise.

--Nasser

Subject: I have n graphs and I want different color

From: Steven_Lord

Date: 9 Nov, 2012 14:45:40

Message: 4 of 6



"Nasser M. Abbasi" <nma@12000.org> wrote in message
news:k7imno$df1$1@speranza.aioe.org...
> On 11/9/2012 4:29 AM, Nasser M. Abbasi wrote:
>> On 11/9/2012 4:16 AM, Debora wrote:

*snip*

> Another option, if you do not want to specify the list
> of colors like the above, is to you rgb colors, and use
> the country to encode the color values.
>
> something like
>
> x=-pi:pi/10:pi;
> for i=1:10
> plot(x,foo(i),'Color',[1,1/i,0.6])
> end

Related to this, if you have HOLD on, you could use the axes property
ColorOrder.

http://www.mathworks.com/help/matlab/ref/axes_props.html#ColorOrder

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: I have n graphs and I want different color

From: Barry Williams

Date: 9 Nov, 2012 15:49:15

Message: 5 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <k7j4ul$nop$1@newscl01ah.mathworks.com>...
>
>
> "Nasser M. Abbasi" <nma@12000.org> wrote in message
> news:k7imno$df1$1@speranza.aioe.org...
> > On 11/9/2012 4:29 AM, Nasser M. Abbasi wrote:
> >> On 11/9/2012 4:16 AM, Debora wrote:
>
> *snip*
>
>> *snip*
When I have to plot spectra, for example, that show an increase in response over time, I use a small program that I wrote to generate an array of colors that go from *cool* blue to *warm* red.

function Colors = plot_colors(no_plots)
% Generates an n X 3 array of rgb values for multiple plots.
% The colors trend from *coolor* blue to *warmer* red.
% This is useful for showing an increase in response over time.
% Can be used in a loop. Example:
% for n=1:no_plots
% plot(x(n), y(n), 'Color', Colors(n,:))
% end
%
Colors = zeros(no_plots,3);
Colors(:,1) = linspace(0,1,no_plots);
Colors(:,3) = linspace(1,0,no_plots);
half = round(no_plots/2);
Colors(1:half,2)=linspace(0,1,half);
Colors(half+1:no_plots,2)=linspace(1,0,no_plots-half);

I have found it useful for easily visualizing trends in the data where there are a maximum of about 20 plots.
Barry

Subject: I have n graphs and I want different color

From: Steven_Lord

Date: 9 Nov, 2012 19:41:02

Message: 6 of 6



"Barry Williams" <barry.r.williamsnospam@saic.com> wrote in message
news:k7j8lr$9b0$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <k7j4ul$nop$1@newscl01ah.mathworks.com>...
>>
>>
>> "Nasser M. Abbasi" <nma@12000.org> wrote in message
>> news:k7imno$df1$1@speranza.aioe.org...
>> > On 11/9/2012 4:29 AM, Nasser M. Abbasi wrote:
>> >> On 11/9/2012 4:16 AM, Debora wrote:
>>
>> *snip*
>>
>>> *snip*
> When I have to plot spectra, for example, that show an increase in
> response over time, I use a small program that I wrote to generate an
> array of colors that go from *cool* blue to *warm* red.

*snip*

That sounds a bit like the colormap produced by the JET function.

http://www.mathworks.com/help/matlab/ref/colormap.html

The COOL colormap goes from cyan to magenta, so it's another similar map.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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