【问题标题】:Loop with Different Colors in Array using Line function in MATLAB使用 MATLAB 中的 Line 函数循环使用数组中的不同颜色
【发布时间】:2019-03-27 17:34:40
【问题描述】:

我创建了一组颜色,以便在输出不同的绘图时循环。

在代码的某个地方它被破坏了。 我没有收到任何错误,但颜色输出并不代表颜色数组。它为黑色的所有行输出数组中的最后一种颜色。

figure1 = figure('Color','w');

apples = [3 5 6 3 2]
oranges = [2 3 4 5 6]
grapes = [3 4 3 2 5]
count = [1 2 3 4 5]


C = {'b','r','g','m','k'} 
hold on
for i = 1:5

line(count, apples, 'LineStyle','-','LineWidth', 2, ...
        'Marker','x', 'MarkerSize',10, 'Color',C{i},'DisplayName','apples');

  line(count, oranges, 'LineStyle','-', 'LineWidth', 2, ...
        'Marker','.', 'MarkerSize',10, 'Color',C{i},'DisplayName','oranges');  

line(count, grapes, 'LineStyle','-', ...
        'Marker','.', 'MarkerSize',10, 'Color',C{i},'DisplayName','grapes' );
end
hold off

【问题讨论】:

  • 我看到你正在绘制 3 行。为什么颜色索引i 的范围是 1 到 5?
  • 是的,您告诉 MATLAB 以每种颜色绘制每一行,黑色是最后一个,所以它在顶部。你想让它做什么?
  • @beaker,我需要用蓝色绘制第一行,用红色绘制第二行,用绿色绘制第三行。
  • 然后删除循环并将C{i}更改为C{1}C{2}C{3}。您已经复制了情节陈述,无需再次重复。
  • 我需要循环来增加更多的地块。更加自动化。

标签: arrays matlab plot colormap


【解决方案1】:

问题: 正如我在评论中提到的那样,您的索引i 的范围从 1 到 5,并且在所述循环内,您每次绘制三行(总共 15 行)。每次迭代都在更改绘制具有相同颜色的三条线(首先是蓝色,然后是红色,依此类推)。

解决方案:
一般方法是以有组织的方式将绘图和绘图属性分开,从而自动执行所需的部分。

自动着色:下面的代码使用称为 Cmat 的 RGB 值矩阵。
公用线属性:用 @ 调整的公用绘图属性(所有线) 987654327@ 循环。
单行属性:通过switch 语句调整的特定行属性。

我通常喜欢将 DisplayName 保留在 plot 调用中,只是为了便于阅读,但这是我的偏好,尤其是在变量名称更抽象的情况下。

% MATLAB R2017a
figure1 = figure('Color','w');
hold on 

apples = [3 5 6 3 2];
oranges = [2 3 4 5 6];
grapes = [3 4 3 2 5];
count = [1 2 3 4 5];


% C = {'b','r','g','m','k'} 
Cmat = [0 0 1; 1 0 0; 0 1 0; .8 0 .2; 0 0 0];   % use a matrix of RGB values

p(1) = plot(count,apples,'DisplayName','apples')      % give plots handles
p(2) = plot(count,oranges,'DisplayName','oranges')
p(3) = plot(count,grapes,'DisplayName','grapes')

% Cosmetics
for j = 1:3                  
    p(j).LineStyle = '-';        % Common plot property adjustments
    p(j).LineWidth = 2;
    p(j).MarkerSize = 10;
    p(j).Color = Cmat(j,:);
    switch j                     % Specific plot property adjustments
        case 1
            p(j).Marker = 'x';   % Marker type for 1st line
        case 2 
            p(j).Marker = '.';
        case 3
            p(j).Marker = '.';
    end
end    
% legend('show')    % include legend

毫无疑问,还有更有效的解决方案。我鼓励 OP 和未来的读者也从@gnovice 看到这个出色的answer

【讨论】:

  • 真的很有帮助。
【解决方案2】:

您的问题是您正在重复所有三个 lines,每次使用不同的颜色,最后一次使用黑色。您应该在没有这样的循环的情况下绘制每一行:

line(count, apples, 'LineStyle', '-', 'LineWidth', 2, ...
     'Marker', 'x', 'MarkerSize', 10, ...
     'Color', C{1}, 'DisplayName', 'apples');
line(count, oranges, 'LineStyle', '-', 'LineWidth', 2, ...
     'Marker', '.', 'MarkerSize', 10, ...
     'Color', C{2}, 'DisplayName', 'oranges');
line(count, grapes, 'LineStyle', '-', ...
     'Marker', '.', 'MarkerSize', 10, ...
     'Color', C{3}, 'DisplayName', 'grapes');
              % ^
              % Note this difference

或者在这样的循环中调用line

data = {apples, oranges, grapes};         % Cell array of your y data
markers = {'x', '.', '.'};                % Cell array of your markers
names = {'apples', 'oranges', 'grapes'};  % Cell array of your names

for iLine = 1:numel(data)
  line(count, data{iLine}, 'LineStyle', '-', ...
       'Marker', markers{iLine}, 'MarkerSize', 10, ...
       'Color', C{iLine}, 'DisplayName', names{iLine});
end

第三个选项是更新坐标区的ColorOrder property,以便它自动(并重复)循环使用您喜欢的颜色(定义为 RGB 三元组)。请注意,您必须使用 plot 而不是 line 以这种方式创建行:

C = [0 0 1; 1 0 0; 0 1 0; 1 0 1; 0 0 0]; 
set(gca, 'ColorOrder', C);

hold on;
plot(count, apples, 'LineStyle', '-', 'LineWidth', 2, ...
     'Marker', 'x', 'MarkerSize', 10, 'DisplayName', 'apples');
plot(count, oranges, 'LineStyle', '-', 'LineWidth', 2, ...
     'Marker', '.', 'MarkerSize', 10, 'DisplayName', 'oranges');
plot(count, grapes, 'LineStyle', '-', ...
     'Marker', '.', 'MarkerSize', 10, 'DisplayName', 'grapes');

【讨论】:

  • gnovice 很好的解释。
  • 不错!不知道ColorOrder 属性调整选项。感谢分享。非常简洁的选项。 (+1)
猜你喜欢
  • 2021-04-25
  • 2015-12-02
  • 2021-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多