Reset ColorOrder index for plotting in Matlab / Octave

*爱你&永不变心* 提交于 2019-12-03 12:27:33

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

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

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

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.

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');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!