Reset ColorOrder index for plotting in Matlab / Octave

24,529

Solution 1

You can shift the original ColorOrder in current axes so that the new plot starts from the same color:

h=plot(x1');
set(gca, 'ColorOrder', circshift(get(gca, 'ColorOrder'), numel(h)))
plot(x2');

You can wrap it in a function:

function h=plotc(X, varargin)
h=plot(X, varargin{:});
set(gca, 'ColorOrder', circshift(get(gca, 'ColorOrder'), numel(h)));
if nargout==0,
    clear h
end
end

and call

hold all
plotc(x1')
plotc(x2')
plotc(x3')

Solution 2

Starting from R2014b there's a simple way to restart your color order.

Insert this line every time you need to reset the color order.

set(gca,'ColorOrderIndex',1)

or

ax = gca;
ax.ColorOrderIndex = 1;

see: http://au.mathworks.com/help/matlab/graphics_transition/why-are-plot-lines-different-colors.html

Solution 3

Define a function that intercepts the call to plot and sets 'ColorOrderIndex' to 1 before doing the actual plot.

function plot(varargin)
if strcmp(class(varargin{1}), 'matlab.graphics.axis.Axes')
    h = varargin{1}; %// axes are specified
else
    h = gca; %// axes are not specified: use current axes
end
set(h, 'ColorOrderIndex', 1) %// reset color index
builtin('plot', (varargin{:})) %// call builtin plot function

I have tested this in Matlab R2014b.

Solution 4

I found a link where a guy eventually solves this. He uses this code:

t = linspace(0,1,lineCount)';
s = 1/2 + zeros(lineCount,1);
v = 0.8*ones(lineCount,1);
lineColors = colormap(squeeze(hsv2rgb(t,s,v)))
ax=gca
ax.ColorOrder = lineColors;

Which should work for you assuming each of your matrices has the same number of lines. If they don't, then I have a feeling you're going to have to loop and plot each line separately using lineColors above to specify an RBG triple for the 'Color' linespec property of plot. So you could maybe use a function like this:

function h = plot_colors(X, lineCount, varargin)

    %// For more control - move these four lines outside of the function and make replace lineCount as a parameter with lineColors
    t = linspace(0,1,lineCount)';                              %//'
    s = 1/2 + zeros(lineCount,1);
    v = 0.8*ones(lineCount,1);
    lineColors = colormap(squeeze(hsv2rgb(t,s,v)));


    for row = 1:size(X,1)
        h = plot(X(row, :), 'Color', lineColors(row,:), varargin{:}); %// Assuming I've remembered how to use it correctly, varargin should mean you can still pass in all the normal plot parameters like line width and '-' etc
        hold on;
    end

end

where lineCount is the largest number of lines amongst your x matrices

Solution 5

If you want a slightly hacky, minimal lines-of-code approach perhaps you could plot an appropriate number of (0,0) dots at the end of each matrix plot to nudge your colourorder back to the beginning - it's like Mohsen Nosratinia's solution but less elegant...

Assuming there are seven colours to cycle through like in matlab you could do something like this

% number of colours in ColorOrder
nco = 7;
% plot matrix 1
plot(x1');
% work out how many empty plots are needed and plot them
nep = nco - mod(size(x1,1), nco); plot(zeros(nep,nep));
% plot matrix 2
plot(x2');
...
% cover up the coloured dots with a black one at the end
plot(0,0,'k');
Share:
24,529
Honza
Author by

Honza

Updated on October 16, 2020

Comments

  • Honza
    Honza over 3 years

    I have matrices x1, x2, ... containing variable number of row vectors. I do successive plots

    figure
    hold all % or hold on
    plot(x1')
    plot(x2')
    plot(x3')
    

    Matlab or octave normally iterates through ColorOrder and plot each line in different color. But I want each plot command to start again with the first color in colororder, so in default case the first vector from matrix should be blue, second in green, third in red etc.

    Unfortunately I cannot find any property related to the color index niether another method to reset it.

  • Honza
    Honza almost 9 years
    Nice solution. I believe that it would work in current Matlab but unfortunately Octave does not know 'ColorOrderIndex' property.
  • Luis Mendo
    Luis Mendo almost 9 years
    Oh. Pity :-) I don't have access to Octave
  • taciteloquence
    taciteloquence almost 4 years
    It seems that this no longer works as of MATLAB 2020a. The answer from Luis Mendo using set(h,'ColorOrderIndex', 1) seems to work though.
  • sancho.s ReinstateMonicaCellio
    sancho.s ReinstateMonicaCellio over 3 years
    Octave v5.1.0 does have ColorOrderIndex.
  • Georg W.
    Georg W. about 3 years
    This answer does not work in Octave, since ColorOrderIndex property is not used in octave.