2

MATLAB 用户的快速“方便”问题。我正在循环一个绘图命令,每次都将不同的数据传递给它。数据恰好是从函数调用中生成的,在每次迭代时都会传递一个不同的参数值。为了在同一轴上绘制所有内容,我使用了“保持”功能。不幸的是,这不会自动循环通过可用的 ColorOrder 和/或 LineStyleOrder 绘图参数,因此绘制的每条线在每次迭代时都具有相同的样式。

for i=1:nLines
    [x_data y_data]=get_xy_data(param1(i),param2(i))
    plot(x_data,y_data)
end

绘制的每条线都将是默认的蓝色线条样式。显而易见的解决方案是预先生成一个包含各种线条样式和颜色的元胞数组,如下所示:

line_styles={'-','--','-*'}; %...etc
colors=colormap(jet(nLines));

然后在每次迭代中访问其中的每一个。我想要的是访问将从 ColorOrder 生成的默认颜色,以及来自 LineStyleOrder 的默认行循环。如果我尝试类似:

get(gca,'LineStyleOrder')

这仅返回该轴中使用的样式(我仅在使用其中一种样式定义的轴上对此进行了测试,但重点是,它并没有给我所有可能的线条样式)。帮助表示赞赏,谢谢!

编辑:让我更具体地说明我在寻找什么。

figure; hold on;
for i=1:nLines
    [xdata, ydata]=get_data(p1(i),p2(i))  % call some function to return x,y data
    plot(xdata,ydata) % on i=1, default blue line

    % function which tells matlab to get/set the next linestyle, color combination          
    nextStyle()       
end

如果这不存在,写它不会太难,但我想我会在重新发明轮子之前先问清楚。

4

3 回答 3

1

您可以使用hold all. 这会自动为每个绘图设置不同的颜色和线型。

于 2013-07-23T19:07:40.023 回答
1

您可以直接为每条线设置线型和颜色。这是一个例子:

figure
hold on
nLines = 12;

line_styles={'-','--','-.'};
colors= hsv(nLines);
indexColors = 1;
indexLines = 1;

for i=1:nLines
    xData = 1:10;
    yData = rand(1,10);
    h = plot(xData,yData);

    ls = line_styles{indexLines};
    c = colors(indexColors,:);

    set(h,'color',c)
    set(h,'LineStyle',ls)

    if indexColors < length(colors)
        indexColors = indexColors + 1;
    else
        indexColors = 1;
    end

    if indexLines < length(line_styles)
        indexLines = indexLines + 1;
    else
        indexLines = 1;
    end
end
于 2013-07-23T19:51:16.173 回答
1

您可能对设置 和 的默认属性DefaultAxesLineStyleOrder感兴趣DefaultAxesColorOrder

绘图(样式和颜色)将首先循环通过新定义的颜色,然后更改线条样式。在连续的绘图循环中,使用hold all将“保持图形和当前线条颜色和线条样式,以便后续绘图命令不会重置 ColorOrder 和 LineStyleOrder”(参见matlab 文档)。这两个示例产生相同的结果。

%default properties (line style and color)  
set(0,'DefaultAxesLineStyleOrder',{'--','-',':'})  
set(0,'DefaultAxesColorOrder', summer(4))  

figure('Color','w');  

%example plot 1 (concurrent plots)  
subplot(1,2,1);  
yvals = [1:50;1:50]  
plot(yvals, 'LineWidth', 2)  
axis([1 2 0 size(yvals,2)+1 ]);  
title('concurrent plot','FontSize',16);  

%example plot 2 (iterative plots)
subplot(1,2,2);  
for ii = 1:50  
    plot(yvals(:,ii), 'LineWidth', 2);  
    hold all;  
end  
axis([1 2 0 size(yvals,2)+1 ]);  
title('successive plot','FontSize',16);  

结果是

在此处输入图像描述

看起来@Luis Mendo 并没有那么错!

于 2013-07-23T22:02:01.960 回答