3

I have created an array of colors to loop over when outputting different plots.

Somewhere in the code it is broken. I am getting no errors, but yet the colors output does not represent the color array. It outputs the last color in the array for all lines in the color black.

figure1 = figure('Color','w');

apples = [3 5 6 3 2]
oranges = [2 3 4 5 6]
grapes = [3 4 3 2 5]
count = [1 2 3 4 5]


C = {'b','r','g','m','k'} 
hold on
for i = 1:5

line(count, apples, 'LineStyle','-','LineWidth', 2, ...
        'Marker','x', 'MarkerSize',10, 'Color',C{i},'DisplayName','apples');

  line(count, oranges, 'LineStyle','-', 'LineWidth', 2, ...
        'Marker','.', 'MarkerSize',10, 'Color',C{i},'DisplayName','oranges');  

line(count, grapes, 'LineStyle','-', ...
        'Marker','.', 'MarkerSize',10, 'Color',C{i},'DisplayName','grapes' );
end
hold off

enter image description here

5
  • 1
    I see you're plotting 3 lines. Why does color index i range from 1 to 5? Commented Mar 27, 2019 at 17:43
  • 1
    Yes, you told MATLAB to plot each line in every color, and black was the last one so it's on top. What did you want it to do? Commented Mar 27, 2019 at 17:44
  • @beaker, I need to plot the first line with color blue, second line with color red, and third line with color green. Commented Mar 27, 2019 at 17:46
  • Then remove the loop and change C{i} to C{1}, C{2} and C{3}. You've already duplicated the plot statements, you don't need to repeat them again. Commented Mar 27, 2019 at 17:48
  • I need the loop for future growth of more plots. More automated. Commented Mar 27, 2019 at 17:50

2 Answers 2

3

Issue: As alluded to in my comment, your index i ranges from 1 to 5 and inside said loop you plot three lines each time (15 total lines). Each iteration is changing plotting three lines with the same color (first blue, then red, and so on).

Solution:
General approach is to separate the plot and the plot properties in organized way that automates desired portions.

Automate color: Code below uses a matrix of RGB values called Cmat.
Common line properties: Common plot properties (all lines) adjusted with for loop.
Individual line properties: Specific line properties adjusted via switch statement.

I generally like to leave the DisplayName with the plot call just for readability but that's my preference, especially if variable names are more abstract.

% MATLAB R2017a
figure1 = figure('Color','w');
hold on 

apples = [3 5 6 3 2];
oranges = [2 3 4 5 6];
grapes = [3 4 3 2 5];
count = [1 2 3 4 5];


% C = {'b','r','g','m','k'} 
Cmat = [0 0 1; 1 0 0; 0 1 0; .8 0 .2; 0 0 0];   % use a matrix of RGB values

p(1) = plot(count,apples,'DisplayName','apples')      % give plots handles
p(2) = plot(count,oranges,'DisplayName','oranges')
p(3) = plot(count,grapes,'DisplayName','grapes')

% Cosmetics
for j = 1:3                  
    p(j).LineStyle = '-';        % Common plot property adjustments
    p(j).LineWidth = 2;
    p(j).MarkerSize = 10;
    p(j).Color = Cmat(j,:);
    switch j                     % Specific plot property adjustments
        case 1
            p(j).Marker = 'x';   % Marker type for 1st line
        case 2 
            p(j).Marker = '.';
        case 3
            p(j).Marker = '.';
    end
end    
% legend('show')    % include legend

Line chart with different colors

Undoubtedly there are more efficient solutions. I encourage OP and future readers to also see this excellent answer from @gnovice.

Sign up to request clarification or add additional context in comments.

1 Comment

Really a great help.
3

Your problem is that you are replotting all three of your lines repeatedly, each time with a different color, the last time with black. You should either plot each line without a loop like this:

line(count, apples, 'LineStyle', '-', 'LineWidth', 2, ...
     'Marker', 'x', 'MarkerSize', 10, ...
     'Color', C{1}, 'DisplayName', 'apples');
line(count, oranges, 'LineStyle', '-', 'LineWidth', 2, ...
     'Marker', '.', 'MarkerSize', 10, ...
     'Color', C{2}, 'DisplayName', 'oranges');
line(count, grapes, 'LineStyle', '-', ...
     'Marker', '.', 'MarkerSize', 10, ...
     'Color', C{3}, 'DisplayName', 'grapes');
              % ^
              % Note this difference

Or call line once inside a loop like this:

data = {apples, oranges, grapes};         % Cell array of your y data
markers = {'x', '.', '.'};                % Cell array of your markers
names = {'apples', 'oranges', 'grapes'};  % Cell array of your names

for iLine = 1:numel(data)
  line(count, data{iLine}, 'LineStyle', '-', ...
       'Marker', markers{iLine}, 'MarkerSize', 10, ...
       'Color', C{iLine}, 'DisplayName', names{iLine});
end

A third option is to update the ColorOrder property of the axes so that it automatically (and repeatedly) cycles through your preferred colors (defined as RGB triples). Note that you'll have to use plot instead of line to create your lines this way:

C = [0 0 1; 1 0 0; 0 1 0; 1 0 1; 0 0 0]; 
set(gca, 'ColorOrder', C);

hold on;
plot(count, apples, 'LineStyle', '-', 'LineWidth', 2, ...
     'Marker', 'x', 'MarkerSize', 10, 'DisplayName', 'apples');
plot(count, oranges, 'LineStyle', '-', 'LineWidth', 2, ...
     'Marker', '.', 'MarkerSize', 10, 'DisplayName', 'oranges');
plot(count, grapes, 'LineStyle', '-', ...
     'Marker', '.', 'MarkerSize', 10, 'DisplayName', 'grapes');

2 Comments

gnovice really nice explanation.
Nice! Didn't know about ColorOrder property adjustment option. Thanks for sharing. Very succinct options. (+1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.