Search code examples
matlabgraphicsformattingmatlab-figure

Change existing plot lines according to colormap


I have a plot with a few lines in Matlab and I want to control the line colors post-hoc:

figure; hold on;
for ind=1:4
  plot(rand(1,10))
end

I know I can use

set(0,'DefaultAxesColorOrder',summer(4))

before plotting, to change the plot line colors, but (how) can I achieve the same thing after looking at the plot? Possibly try-out a few different colorschemata?


Solution

  • Each plot by default takes its color from the property 'ColorOrder' of their axis, which in turn is taken by default from the 'DefaultAxesColorOrder' of the groot object.

    Once the plots have been created, you need to modify their colors individually; changing the above mentioned properties will not affect them:

    g = findobj(gca, 'Type', 'line'); % children of current axis that are lines
    c = summer(numel(g)); % desired color scheme, with that many colors
    for k = 1:numel(g)
        set(g(k), 'color', c(k,:));
    end