5

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

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

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)

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

再次感谢

4

2 回答 2

8

只需使用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,即每次绘制时更改绘图的颜色。文档声明该hold all语法将在未来版本中删除。

于 2013-04-26T12:33:26.960 回答
7

怎么样:

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

阴谋

于 2013-04-26T12:57:40.863 回答