Reset ColorOrder index for plotting in Matlab / Octave

前端 未结 5 1290
独厮守ぢ
独厮守ぢ 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: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

提交回复
热议问题