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:
Multiple polar plots on same figure - different colors for each plot?

Subject: Multiple polar plots on same figure - different colors for each plot?

From: Stan

Date: 10 Nov, 2012 01:48:17

Message: 1 of 7

I am trying to produce several polar plots on the same graph, using a loop. Each plot will have a different color.i.e. one color per iteration. However, I am having problems with the color specification.

Here is what I have:

for j = 1:10
   .
   .
   .
   a=polar(x,y,color)
   legend(a)
end

Questions:
a. Is there a way to specify a separate color for each iteration of the loop?
b. Is there a way to display a legend showing all the colors in the figure?

Subject: Multiple polar plots on same figure - different colors for each

From: Nasser M. Abbasi

Date: 10 Nov, 2012 01:55:05

Message: 2 of 7

On 11/9/2012 7:48 PM, Stan wrote:
> I am trying to produce several polar plots on the same graph, using a loop.
>Each plot will have a different color.i.e. one color per iteration. However,
>I am having problems with the color specification.
>
> Here is what I have:
>
> for j = 1:10
> .
> .
> .
> a=polar(x,y,color)
> legend(a)
> end
>
> Questions:
> a. Is there a way to specify a separate color for each iteration of the loop?
> b. Is there a way to display a legend showing all the colors in the figure?
>

someone was asking similar question yesterday, here is the thread

"Thread Subject: I have n graphs and I want different color"

http://www.mathworks.com/matlabcentral/newsreader/view_thread/324383

few suggestions there. See the thread, and if still have question
you can ask

Subject: Multiple polar plots on same figure - different colors for each

From: Stan

Date: 10 Nov, 2012 02:15:09

Message: 3 of 7

 >
> > Questions:
> > a. Is there a way to specify a separate color for each iteration of the loop?
> > b. Is there a way to display a legend showing all the colors in the figure?
> >
>
> someone was asking similar question yesterday, here is the thread
>
> "Thread Subject: I have n graphs and I want different color"
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/324383
>
> few suggestions there. See the thread, and if still have question
> you can ask
>

I liked your response in that thread:
x=-pi:pi/10:pi;
for i=1:10
   plot(x,foo(i),'Color',[1,1/i,0.6])
end

However, in a polar plot, I cannot specify 'Color'. ex. the following command will not work:
   polar(x,foo(i),'Color',[1,1/i,0.6])

This is the problem that I am having.

Am I missing something?

Subject: Multiple polar plots on same figure - different colors for each

From: Nasser M. Abbasi

Date: 10 Nov, 2012 02:30:36

Message: 4 of 7

On 11/9/2012 8:15 PM, Stan wrote:
> >
>>> Questions:
>>> a. Is there a way to specify a separate color for each iteration of the loop?
>>> b. Is there a way to display a legend showing all the colors in the figure?
>>>
>>
>> someone was asking similar question yesterday, here is the thread
>>
>> "Thread Subject: I have n graphs and I want different color"
>>
>> http://www.mathworks.com/matlabcentral/newsreader/view_thread/324383
>>
>> few suggestions there. See the thread, and if still have question
>> you can ask
>>
>
> I liked your response in that thread:
> x=-pi:pi/10:pi;
> for i=1:10
> plot(x,foo(i),'Color',[1,1/i,0.6])
> end
>
> However, in a polar plot, I cannot specify 'Color'. ex. the following command will not work:
> polar(x,foo(i),'Color',[1,1/i,0.6])
>
> This is the problem that I am having.
>
> Am I missing something?
>

try set() instead, it will work.

-------------------------------------

t = 0 : .01 : 2 * pi;
h= polar(t, sin(2 * t) .* cos(2 * t));
set(h,'Color',[1 1 1]) %change the rgb code based on index value
                        %make a mapping as needed to encode

--------------------------------


--Nasser

Subject: Multiple polar plots on same figure - different colors for each

From: Stan

Date: 10 Nov, 2012 02:43:17

Message: 5 of 7


> set(h,'Color',[1 1 1]) %change the rgb code based on index value
> %make a mapping as needed to encode

Okay, Thank you. That did work.

My loop has i = 1:10. I tried [1, 1/i, 0.9]. However, this gave the same color for all 10 graphs.

How would suggest that I set up the rgb values and encoding?

Subject: Multiple polar plots on same figure - different colors for each

From: Nasser M. Abbasi

Date: 10 Nov, 2012 02:54:05

Message: 6 of 7

On 11/9/2012 8:43 PM, Stan wrote:
>
>> set(h,'Color',[1 1 1]) %change the rgb code based on index value
>> %make a mapping as needed to encode
>
> Okay, Thank you. That did work.
>
> My loop has i = 1:10. I tried [1, 1/i, 0.9]. However, this gave the same color for all 10 graphs.
>
> How would suggest that I set up the rgb values and encoding?
>

That was just an example. It gives the 'same' color, since
there was very little difference in the rgb code.

You need to figure out the rgb code to use based on the loop
index. You need to make a function which takes an integer
(the index), and returns a 3 element vector (the rgb code).

Based on the number of plots (maximum index), you should
be able to scale the rgb code so that it is uniformly
distributed from 0 to 1 over all the plots. It should be
easy to set up this function.

I do not know if 'ColorOrder' can be used to do this or not,
but you can look into it also. I did not use 'ColorOrder'
myself.

--Nasser

Subject: Multiple polar plots on same figure - different colors for each

From: Nasser M. Abbasi

Date: 10 Nov, 2012 06:49:07

Message: 7 of 7

On 11/9/2012 8:43 PM, Stan wrote:
>
>> set(h,'Color',[1 1 1]) %change the rgb code based on index value
>> %make a mapping as needed to encode
>
> Okay, Thank you. That did work.
>
> My loop has i = 1:10. I tried [1, 1/i, 0.9]. However, this gave the same color for all 10 graphs.
>
> How would suggest that I set up the rgb values and encoding?
>


You can try something as simple as linspace() to get the coding
for the rgb based on the index:

------------------------
figure(1);
nRow = 3;
nCol = 2;
N = nRow*nCol;
t = 0 : .01 : 2 * pi;
z = linspace(0,1,N);

for i = 1:N
     subplot(nRow,nCol,i);
     h = polar(t, sin(2 * t) .* cos(2 * t));
     set(h,'Color',[1-z(i) .5 z(i)]);
end
-------------------------

You can play around to see if you can get 'nicer' colors
that what I have above.

screen shot:

http://12000.org/tmp/nov_10_2012/auto_colors_matlab.png

--Nasser

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