4

I would like to be able to choose the colors for a multiline plot but I can not get it. This is my code

colors = {'b','r','g'};
T = [0 1 2]';
column = [2 3];
count = magic(3);
SelecY = count(:,column),
plot(T,SelecY,'Color',colors{column});
julianfperez
  • 1,726
  • 5
  • 38
  • 69
  • 1
    Also check out this Q/A: http://stackoverflow.com/questions/2028818/automatically-plot-different-colored-lines-in-matlab – John Colby Nov 03 '11 at 23:03

2 Answers2

5

For some reason I couldn't get it to work without using a handle, but:

h = plot(T,SelecY);
set(h, {'Color'}, colors(column)');

Works for me.

dantswain
  • 5,427
  • 1
  • 29
  • 37
3

You can only specify one color at a time that way, and it must be specified as a 3-element RGB vector. Your three routes are:

  1. Loop through and specify the colors by string, like you have them:

    hold on
    for i=1:size(SelecY, 2)
        plot(T, SelecY(:,i), colors{i});
    end
    
  2. Using the RGB color specification, you can pass the colors in via the 'Color' property, like you were trying to do above:

    cols = jet(8);
    hold on
    for i=1:size(SelecY, 2)
        plot(T, SelecY(:,i), 'Color', cols(i,:));
    end
    
  3. Also using the RGB way, you can specify the ColorOrder up front, and then let matlab cycle through:

    set(gca, 'ColorOrder', jet(3))
    hold all
    for i=1:size(SelecY, 2)
        plot(T, SelecY(:,i));
    end
    

For setting colors after the fact, see the other answer.

John Colby
  • 22,169
  • 4
  • 57
  • 69