【问题标题】:Changing color of plot lines in a loop在循环中更改绘图线的颜色
【发布时间】:2016-07-25 20:00:36
【问题描述】:

我有一个关于在 MATLAB 中更改绘图中线条颜色的问题。我编写了一个代码,我想在其中更改嵌入在多个 for 循环中的绘图中线条的颜色(代码如下所示)。在代码中,当满足“if 循环”条件时,将创建一个新图(带有自己的图)。我希望每个图都有不同颜色的线条(来自其他图),所以我创建了一个变量 =“NewColor”来增加和更改线条颜色。但是,一直出现以下问题:

假设我处于调试模式,并且在 plot 命令处停止。我运行下一步,并用蓝线创建了一个图。我检查 NewColor 的值,发现 NewColor = 0.1。接下来,我使用“运行到光标”命令跳到下一次激活绘图命令时。当我这样做时,我仍然在“i for loop”中,所以 NewColor 没有改变。我检查编辑器发现 NewColor = 0.1。因此,当我运行下一步时,当前绘图上应该会出现一条蓝线。相反,我难以置信地出现了一条橙色线(除了蓝线)。我不明白,因为在调试器的两个步骤中 NewColor = 0.1,并且代码是这样编写的,所以线条的颜色 = [0,NewColor,0]。如果有人能找到我的方式的错误,将不胜感激。谢谢

ThetaPlot = [40,50];   % Put incident angle as input

Count1 = 0;
Count2 = 0;
NewColor = 0;

for m = 1:length(ThetaPlot)
    NewColor = 0.1;
    Title = sprintf('Angle(%d)',ThetaPlot(m));
    figure('name',Title)
    Count1 = 0;
    for i = 1:length(xrange)-1             % X Coordinate of Start Node
        for j = 1:length(yrange)-1         % Y Coordinate of Start Node
            Count1 = Count1+1;
            for k = 2:length(xrange)       % X Coordinate of End Node
                for l = 2:length(yrange)   % Y Coordinate of End Node
                    Count2 = Count2+1;
                    if ReflRayData(Count2,ThetaPlot(m) - ThetaIncident(1) + 1,Count1) == 1
                        x = [xrange(i),xrange(k)];
                        y = [yrange(j),yrange(l)];
                        plot(x,y,['-',[0,NewColor,0],'o']);
                        hold on;
                    end
                end
                Count2 = 0;
            end
        end
    end
    NewColor = NewColor + 0.02;
end

完整代码:

%% Calculating Angles of Reflection

run = 1; % Set run = 1 for calculations

if run == 1
    xrange = [0:1:14.5]';      % Coordinates to try for Panel Geometry (in)
    yrange = [0:1:36]';        % Coordinates to try for Panel Geometry (in)
    ThetaIncident = [-90:1:90]';     % Incident Angle of Ray (measured relative to normal direction with clockwise postive)
    OvenGlassXrange = [14.5:0.1:36.5]; %Range of X coordinates for Oven Glass

ReflRayData = zeros((length(xrange)-1)*(length(yrange)-1),length(ThetaIncident),(length(xrange)-1)*(length(yrange)-1));  % Matrix containing Reflected Ray Data

Count1 = 0;
Count2 = 0;
for i = 1:length(xrange)-1             % X Coordinate of Start Node
    for j = 1:length(yrange)-1         % Y Coordinate of Start Node
        Count1 = Count1+1;
        for k = 2:length(xrange)       % X Coordinate of End Node
            for l = 2:length(yrange)   % Y Coordinate of End Node
                Count2 = Count2+1;
                for m = 1:length(ThetaIncident)
                    xStart = xrange(i);
                    yStart = yrange(j);
                    xEnd = xrange(k);
                    yEnd = yrange(l);

                    m1 = (yEnd - yStart)/(xEnd - xStart);  % Slope between Start and End Nodes
                    b1 = yStart - m1*xStart;

                    m2 = 1/m1;                             % Slope of normal direction

                    b2 = (yEnd - 0.5*(yEnd - yStart)) - m2*(xEnd - 0.5*(xEnd - xStart));

                    ArbXCoor = 1;                          % Arbitary Point X Coordinate on Normal Line
                    ArbYCoor = m2*ArbXCoor+b2;             % Arbitary Point Y Coordinate on Normal Line

                    ThetaReflected = -ThetaIncident(m);       % Reflected Angle

                    ArbXCoorRot = ArbXCoor*cosd(ThetaReflected) - ArbYCoor*sind(ThetaReflected); % Arbitary Point X Coordinate on Reflected Line
                    ArbYCoorRot = ArbYCoor*cosd(ThetaReflected) + ArbXCoor*sind(ThetaReflected); % Arbitary Point Y Coordinate on Reflected Line

                    m3 = (ArbYCoorRot - (yEnd - 0.5*(yEnd - yStart)))/(ArbXCoorRot - (xEnd - 0.5*(xEnd - xStart))); % Slope of Reflected Line
                    b3 = (yEnd - 0.5*(yEnd - yStart)) - m3*(xEnd - 0.5*(xEnd - xStart));

                    ElemLength = sqrt((yEnd - yStart)^2 + (xEnd - xStart)^2);

                    if min(OvenGlassXrange) < -b3/m3 && -b3/m3 < max(OvenGlassXrange) && -1 < m1 && m1 < 0 && m1 ~= -Inf && m1 ~= Inf && ElemLength < 3
                        ReflRayData(Count2,m,Count1) = 1;
                    end 
                end
            end
        end
        Count2 = 0;
    end
end

%% Plotting

ThetaPlot = [40,50];   % Put incident angle as input

Count1 = 0;
Count2 = 0;
NewColor = 0;

for m = 1:length(ThetaPlot)
    NewColor = 0.1;
    Title = sprintf('Angle(%d)',ThetaPlot(m));
    figure('name',Title)
    Count1 = 0;
    for i = 1:length(xrange)-1             % X Coordinate of Start Node
        for j = 1:length(yrange)-1         % Y Coordinate of Start Node
            Count1 = Count1+1;
            for k = 2:length(xrange)       % X Coordinate of End Node
                hold on;
                for l = 2:length(yrange)   % Y Coordinate of End Node
                    Count2 = Count2+1;
                    if ReflRayData(Count2,ThetaPlot(m) - ThetaIncident(1) + 1,Count1) == 1
                        x = [xrange(i),xrange(k)];
                        y = [yrange(j),yrange(l)];
                        plot(x,y,['-',[0,NewColor,0],'o']);
                        hold on;
                    end
                end
                Count2 = 0;
            end
        end
    end
    NewColor = NewColor + 0.02;
end

【问题讨论】:

  • 您的许多变量未定义。请提供minimal reproducible example
  • 请试着把'图;坚持;'在你的'for'循环之前并在你的循环中删除那个'hold on'。如果可行,我会给你写一个完整的答案
  • 请解释您的plot 电话。 ['-',[0,NewColor,0],'o'] 作为 plot 的参数毫无意义。
  • [0,NewColor,0] 的想法是可以在 matlab 中通过它们的 rgb 值以 [r,g,b] 格式定义颜色。例如,[1,1,0] 是黄色的。
  • 另外,根据过去的经验,我知道定义 rgb 颜色、指定线型和指定标记类型的唯一方法是使用我在代码中的格式。

标签: matlab for-loop colors


【解决方案1】:

代替plot(x,y,['-',[0,NewColor,0],'o']);试试:

plot(x,y,'linestyle','-','marker','o','color',[0,NewColor,0])

【讨论】:

  • 另外,我是否建议使用带有升序参数的 hsv2rgb 作为更改“色调”而不是单个 r、g 或 b 通道强度的非常好的方法。我最近回答了一个问题,我举了这样一个例子:stackoverflow.com/questions/38550379/… ...我认为你的颜色变化会更好:)
【解决方案2】:

根据 Matlab 文档,通过调用 hold on Matlab 使用下一种颜色。

hold on 保留当前坐标区中的图,以便添加到坐标区的新图不会删除现有图。新绘图使用基于轴的 ColorOrder 和 LineStyleOrder 属性的下一个颜色和线型。

因此,在您的代码中,当您在 for 内调用 hold on 时,它只使用下一种颜色。

我的解决方案是将figure; hold on; 放在for 循环之前并在循环中删除那个

【讨论】:

  • 他试图通过每个plot 调用明确地传递颜色。 hold 对轴的下一种颜色的影响不相关。此外,重复调用hold on 与调用一次对行为没有影响。
猜你喜欢
  • 2017-08-01
  • 2012-04-26
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多