Reset ColorOrder index for plotting in Matlab / Octave

前端 未结 5 1282
独厮守ぢ
独厮守ぢ 2021-02-07 15:47

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

figure
hold all % or hold on
plot(x1\'         


        
相关标签:
5条回答
  • 2021-02-07 16:07

    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

    0 讨论(0)
  • 2021-02-07 16:14

    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

    0 讨论(0)
  • 2021-02-07 16:15

    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.

    0 讨论(0)
  • 2021-02-07 16:26

    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');
    
    0 讨论(0)
  • 2021-02-07 16:31

    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')
    
    0 讨论(0)
提交回复
热议问题