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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How to use same colors inn all figures when making several figures with a loop

Asked by Karl on 13 May 2013

I have created a loop that makes several figures. When I apply "rand(1,3)) in the loop below, each figure gets different colors. Do you know how I can change the loop below to so that the colors are the same in every figure?

Alder = {'<20', '2029','3039','4049','5059','6069','>70', 'all'};
Vars = {gjG, gjR, gjI, gjDI, gjBV, GtoDI, GtoBV, RtoDIplusR, ItoG, RtoG, GtoI};
Vars2 = {'gjG', 'gjR', 'gjI', 'gjDI', 'gjBV', 'GtoDI', 'GtoBV', 'RtoDIplusR', 'ItoG', 'RtoG', 'GtoI'};
Aar = {'2011', '2012', '2013_1', '2014_1', '2014_s2', '2014_s5'};
n = length(Vars);
nAar = length(Aar);
for iVars = 1:n;
  aVars = Vars{iVars};
figure,title(Vars2{iVars}); 
hold on
for iAar = 1:nAar
  plot(aVars(11,:,iAar), 'color', rand(1,3));
set(gca, 'XTickLabel',Alder)
legend((Aar),'location','NE','FontSize',10);
end
hold off
end

0 Comments

Karl

Tags

Products

No products are associated with this question.

3 Answers

Answer by the cyclist on 13 May 2013
Accepted answer

This line sets the color:

 plot(aVars(11,:,iAar), 'color', rand(1,3))

You could change that to

   plot(aVars(11,:,iAar), 'color', 'r')

for red, or

plot(aVars(11,:,iAar), 'color', [r g b])

where r,g, and b are numeric values to give any RGB color.

5 Comments

the cyclist on 13 May 2013

Randomly generated colors are often difficult to distinguish from each other. If you want to get more sophisticated, consider using this entry from the File Exchange:

http://www.mathworks.com/matlabcentral/fileexchange/29702-generate-maximally-perceptually-distinct-colors

Karl on 13 May 2013

What you suggested in the first post worked. Thanks! You are right about the colors.Not much difference!

the cyclist on 13 May 2013

You can try setting different random seeds at the beginning of your code, to see if you can find a better array of colors.

If you don't want to use random colors, you can try the approach that Image Analyst had in his answer, which is similar to my answer but takes advantage of built-in MATLAB color maps.

the cyclist
Answer by Azzi Abdelmalek on 13 May 2013

Use

plot(aVars(11,:,iAar), 'color', [1 0 1]);

1 Comment

Karl on 13 May 2013

What you suggest give the same color for alle graphs in the same figure. I want different colors for each label in the figures, but that the labels in the different figures have the same colors. So "2011" should have a different color from "2012" ion each figure, and the "2011"-color should be equall accross all figures.

Azzi Abdelmalek
Answer by Image Analyst on 13 May 2013

Make up a table of colors. It should be N rows (for N colors that you want to use) by 3 columns (a red, a green, and a blue value). The numbers should go from (0,0,0) for black up to (1,1,1) for white. If you want, you can use some built in colormaps, like lines:

numberOfColors = 15; % Whatever you want.
myColorMap = lines(numberOfColors);  % Use lines(), jet(), winter(), or whatever.

Whenever you want to plot the curve in the k'th color, you just do

plot(x, y, 'Color', myColorMap(k, :));

Do this for every axes that you want to plot in.

1 Comment

Karl on 13 May 2013

That worked. Thanks!

Image Analyst

Contact us