首页 文章

重置ColorOrder索引以在Matlab / Octave中绘图

提问于
浏览
10

我的矩阵 x1, x2, ... 包含 variable 个行向量 . 我做了连续的情节

figure
hold all % or hold on
plot(x1')
plot(x2')
plot(x3')

Matlab或八度音程通常遍历 ColorOrder 并以不同颜色绘制每条线 . But I want each plot command to start again with the first color in colororder ,所以在默认情况下,矩阵中的第一个矢量应为蓝色,第二个为绿色,第三个为红色等 .

不幸的是我找不到与颜色索引相关的任何属性,而另一种方法是重置它 .

5 回答

  • 2

    您可以在当前轴上移动原始 ColorOrder ,以便新绘图从相同的颜色开始:

    h=plot(x1');
    set(gca, 'ColorOrder', circshift(get(gca, 'ColorOrder'), numel(h)))
    plot(x2');
    

    你可以将它包装在一个函数中:

    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
    

    并打电话

    hold all
    plotc(x1')
    plotc(x2')
    plotc(x3')
    
  • 7

    从R2014b开始,有一种简单的方法可以重新启动颜色顺序 .

    每次需要重置颜色顺序时插入此行 .

    set(gca,'ColorOrderIndex',1)
    

    要么

    ax = gca;
    ax.ColorOrderIndex = 1;
    

    见:http://au.mathworks.com/help/matlab/graphics_transition/why-are-plot-lines-different-colors.html

  • 1

    found a link一个人最终解决了这个问题 . 他使用这段代码:

    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;
    

    假设每个矩阵的行数相同,哪个应该适用于您 . 如果他们不必使用 lineColors 分别循环和绘制每一行,则为 plot'Color' linespec属性指定RBG三元组 . 所以你可以使用这样的函数:

    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
    

    其中 lineCountx 矩阵中最大的行数

  • 2

    在执行实际绘图之前,定义一个拦截对 plot 的调用并将 'ColorOrderIndex' 设置为 1 的函数 .

    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
    

    我在Matlab R2014b中对此进行了测试 .

  • 5

    如果你想要一个稍微hacky,最小的代码行方法,也许你可以在每个矩阵图的末尾绘制适当数量的(0,0)点,以便将你的colourorder推回到开头 - 这就像Mohsen Nosratinia的解决方案但是不太优雅......

    假设有七种颜色可以在matlab中循环,你可以做这样的事情

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

相关问题