13
votes

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.

5
Do all the x matrices plot the same number of lines?Dan
which version of Matlab do you use ? If you use version anterior to 2014b you can simply use hold on instead of hold all ... then all the plot command will start at the beginning of the colour order.Hoki
Actually I use Octave as I do not have Matlab license any more. @Dan I can imagine setting colormap of proper lenght, but unfortunately the number of lines varies. That is the reason for my question.Honza
@Honza then you might have to loop. I'll edit my answer below to create a function for you...Dan
A loop is not elegant but it seems to be the only way, unfortunately...Honza

5 Answers

9
votes

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

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

5
votes

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.

2
votes

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

1
votes

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