MATLAB:设置线条的颜色和样式顺序以并行应用 [英] MATLAB: set line's color and style order to be applied in parallel

查看:794
本文介绍了MATLAB:设置线条的颜色和样式顺序以并行应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置DefaultAxesColorOrderDefaultAxesLineStyleOrder时,MATLAB将首先循环显示第一种样式的所有颜色,然后再次循环显示第二种样式的所有颜色,依此类推.

When you set DefaultAxesColorOrder and DefaultAxesLineStyleOrder MATLAB will first cycle through all colors with the first style, then again through all colors with the second style and so on.

请参见文档我想做的是将颜色顺序和样式顺序设置为独立应用.

What I would like to do is to set color order and style order to be applied independently.

例如,如果我将DefaultAxesColorOrder设置为[1 0 0; 0 1 0; 0 0 1],并且将DefaultAxesLineStyleOrder设置为'-|--|:',则这些行将是'r-''g-''b-''r--''g--''b--''r:''g:''b:'.我希望行为'r-''g--''b:'.

For example, if I set DefaultAxesColorOrder to [1 0 0; 0 1 0; 0 0 1] and DefaultAxesLineStyleOrder to '-|--|:', the lines will be 'r-','g-','b-','r--','g--','b--','r:','g:','b:'. I want lines to be 'r-','g--','b:'.

推荐答案

我看不到直接实现此目的的方法.最直接的方法是为每行手动设置颜色/样式.

I don't see a way to do this directly out of the box. The straightforward way is to set the color/style manually for each line.

这是一种更加自动化的解决方案.让我们先从文档中获取一个示例:

Here is a more automated solution. Let's start with an example taken from the documentation:

%# defaults are set sometime before
set(0, 'DefaultAxesColorOrder',[1 0 0;0 1 0;0 0 1], ...
      'DefaultAxesLineStyleOrder','-|--|:')

%# do plotting as usual
t = 0:pi/20:2*pi;
a = zeros(length(t),9);
for i = 1:9
    a(:,i) = sin(t-i/5)';
end
h = plot(t,a);

正如您在问题中解释的那样,默认行为是先循环显示颜色,然后循环显示线型.如果要独立应用它们,请尝试以下操作:

As you explained in your question, the default behavior is to cycle through the colors first, then the line styles. If you want to apply them independently, try the following:

c = num2cell(get(0,'DefaultAxesColorOrder'),2);
l = cellstr(get(0,'DefaultAxesLineStyleOrder'));
set(h, {'Color'}, c(rem((1:numel(h))-1,numel(c))+1), ...
    {'LineStyle'}, l(rem((1:numel(h))-1,numel(l))+1))

您可以将其包装在一个函数中以方便访问(您仍然必须将句柄传递给线条图形对象):

You can maybe wrap that in a function for convenient access (you still have to pass the handles to the lines graphic objects):

function applyColorLineStyleIndependently(h)
    %# ...
end

这篇关于MATLAB:设置线条的颜色和样式顺序以并行应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆