【问题标题】:How can I change the color of the plot in each iteration in MATLAB?如何在 MATLAB 的每次迭代中更改绘图的颜色?
【发布时间】:2014-12-31 14:51:24
【问题描述】:

以下是我的matlab代码的一部分。如图所示,我想在一张图中绘制 8 条曲线。但我想用一种独特的颜色制作每条曲线。我还想更改图例,以便它为每个i 更改。

例如,对于 i=1,图例将是 gho-1,对于 i=2 gho-2,依此类推。我希望它是自动的,因为我有时会从 ex:(i=1:20) 更改 i

for i=1:8
.
.
.
plot(b,r,'b');
legend(['qho-',num2str(i)]);    
hold on
end

我该怎么做?

你好,

我还有其他问题: 如果我有以下情况

for i=1:8
.
b1=(1:3,:)
b2=(3:6,:)
figure(1);plot(b1,r,'*');
figure(2);plot(b2,r,'*');

Leg{i} = ['qho-',num2str(i)];    

end
legend(Leg)

我只有最后一个数字的颜色图例。不是两个.. 我该如何解决?!

再次感谢

【问题讨论】:

  • 您应该将后续问题作为新问题提出

标签: matlab plot legend


【解决方案1】:

只需使用 hold all 而不是 hold on 并将图例标签放在元胞数组中

hold all
for i=1:8
    .
    .
    .
    plot(b,r);

    Leg{i} = ['qho-',num2str(i)];    

end
legend(Leg)

例如看这个问题:Sparse matrix plot matlab


注意:

Matlab R2014b 开始hold on 已被修改为类似于 hold all,即每次绘制绘图时更改绘图的颜色。 The docs 声明 hold all 语法将在未来的版本中删除。

【讨论】:

  • hold all 确实限制了 7 种颜色,然后循环返回。如果您想定义自己的颜色集,您可以打开您的图和set(gca,'ColorOrder',myColors),其中myColors 是一个 Nx3 RGB 值矩阵。
  • 你好,我还有其他问题:如果我有以下 i=1:8 。 b1=(1:3,:) b2=(3:6,:) 图(1);绘图(b1,r,'');图(2);绘图(b2,r,'');腿{i} = ['qho-',num2str(i)];结束图例(腿)我只有最后一个数字的颜色图例。不适合两者..我该如何解决?!再次感谢
  • Leg1{}Leg2{} 分开,并确保在拨打figure(2) 之前先拨打legend(Leg1)
  • 嗨,我就是这么做的。但只在第一个图中看到不同的图例和颜色,而另一个我只看到一个图例(例如 gho-1)
【解决方案2】:

怎么样:

figure, hold on
N = 8;
h = zeros(N,1);    %# store handle to line graphic objects
clr = lines(N);    %# some colormap
for i=1:N
    %# plot random data
    y = cumsum(randn(100,1));
    h(i) = plot(y, 'Color',clr(i,:));
end
hold off
legend(h, num2str((1:N)','gho-%d'))    %# display legend

【讨论】:

  • 这也很棒。有很多选择的方法:) :).. 非常感谢:)
  • 谢谢!有什么办法可以在循环中使用subplot's? 1 个图上的 2 个子图,每次迭代时都有两个子图,例如
猜你喜欢
  • 2021-03-28
  • 1970-01-01
  • 2020-07-20
  • 2019-08-16
  • 2012-01-23
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 2021-11-20
相关资源
最近更新 更多