Setting colors for plot function in Matlab

11,608

Solution 1

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.

Solution 2

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.

Share:
11,608
julianfperez
Author by

julianfperez

BY DAY: Full Stack Java Developer and MEng Aerospace Engineer. BY NIGHT: Superhero, DJ. FOR FUN: Blogger, Writer, Poet, Actor. “I am not a product of my circumstances. I am a product of my decisions.” - Stephen R. Covey

Updated on June 12, 2022

Comments

  • julianfperez
    julianfperez almost 2 years

    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});