MATLAB:设置要并行应用的线条颜色和样式顺序

发布于 2024-12-03 15:18:40 字数 807 浏览 2 评论 0原文

当您设置 DefaultAxesColorOrderDefaultAxesLineStyleOrder 时,MATLAB 将首先循环使用第一种样式的所有颜色,然后再次循环使用第二种样式的所有颜色,依此类推。

请参阅此文档相关问题

我想做的是设置独立应用的颜色顺序和样式顺序。

例如,如果我将 DefaultAxesColorOrder 设置为 [1 0 0; 0 1 0; 0 0 1]DefaultAxesLineStyleOrder'-|--|:',这些行将是'r-','g-','b-','r--',< code>'g--','b--','r:','g:',<代码>'b:'。我希望行为 'r-''g--''b:'

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.

See this documentation or related question.

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

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:'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆° 2024-12-10 15:18:40

我没有找到直接开箱即用的方法。最简单的方法是手动设置每条线的颜色/样式。

这是一个更加自动化的解决方案。让我们从文档中的示例开始:

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

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

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))

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

function applyColorLineStyleIndependently(h)
    %# ...
end

在此处输入图像描述

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

enter image description here

只是偏爱你 2024-12-10 15:18:40

Amro 的方法效果很好。请注意,您不必设置默认值即可执行此操作。你可以做这样的事情

col = mycolors(); % defines RGB colors scaled to [0,1]

i = 1;
c(:,i) = col.royal_blue; i = i+1;
c(:,i) = col.crimson; i = i+1;
c(:,i) = col.medium_sea_green; i = i+1;
c(:,i) = col.coral; i = i+1;
c(:,i) = col.dark_magenta; i = i+1;

colord = num2cell(c',2);
lineord = {'-' '--' '-.'}'; 

set(h,{'Color'}, colord(rem((1:numel(h))-1,numel(colord))+1), ...
      {'LineStyle'}, lineord(rem((1:numel(h))-1,numel(lineord))+1))
set(h,'LineWidth',2)

编辑:mycolors()函数是自制的。我定义

colors.maroon = [128,0,0];
colors.dark_red = [139,0,0];
colors.brown = [165,42,42];
...

(颜色名称来自此 http://www.rapidtables.com/web/颜色/RGB_Color.htm)。然后我将它们缩放到 [0,1] 通过

c = fieldnames(colors);
for i = 1:numel(c)
    colors.(c{i}) = colors.(c{i})/255;
end

Amro's approach works well. Just as a note, you don't have to set the defaults to do this. You can do something like this

col = mycolors(); % defines RGB colors scaled to [0,1]

i = 1;
c(:,i) = col.royal_blue; i = i+1;
c(:,i) = col.crimson; i = i+1;
c(:,i) = col.medium_sea_green; i = i+1;
c(:,i) = col.coral; i = i+1;
c(:,i) = col.dark_magenta; i = i+1;

colord = num2cell(c',2);
lineord = {'-' '--' '-.'}'; 

set(h,{'Color'}, colord(rem((1:numel(h))-1,numel(colord))+1), ...
      {'LineStyle'}, lineord(rem((1:numel(h))-1,numel(lineord))+1))
set(h,'LineWidth',2)

Edit: the mycolors() function is home made. I define

colors.maroon = [128,0,0];
colors.dark_red = [139,0,0];
colors.brown = [165,42,42];
...

(the color names are from this http://www.rapidtables.com/web/color/RGB_Color.htm). Then I scale them to [0,1] via

c = fieldnames(colors);
for i = 1:numel(c)
    colors.(c{i}) = colors.(c{i})/255;
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文