Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting colors for plot function in Matlab

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});
like image 642
julianfperez Avatar asked Feb 22 '23 12:02

julianfperez


2 Answers

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.

like image 127
dantswain Avatar answered Mar 02 '23 19:03

dantswain


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.

like image 28
John Colby Avatar answered Mar 02 '23 21:03

John Colby