Reset ColorOrder index for plotting in Matlab / Octave

Honza picture Honza · May 12, 2015 · Viewed 17.2k times · Source

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.

Answer

Mohsen Nosratinia picture Mohsen Nosratinia · May 12, 2015

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')