如何在MATLAB的每次迭代中更改图的颜色? [英] How can I change the color of the plot in each iteration in MATLAB?
问题描述
以下是我的matlab代码的一部分.如图所示,我想在一张图中绘制8条曲线.但是我想使每条曲线具有一种独特的颜色.我还想更改图例,以便每个i
都更改.
The following is a part of my matlab code. As it's shown, I would like to plot 8 curves in one plot. But I want to make each curve with one unique color. I also want to change the legend so that it changes for each i
.
例如,对于i = 1,图例将是gho-1,对于i = 2 gho-2,依此类推.我希望它是自动的,因为有时会从ex:(i = 1:20)更改i
.
For instance, for i=1 the legend will be gho-1, for i=2 gho-2 and so on. I want it to be automatic because I will change the i
sometimes from ex:(i=1:20).
for i=1:8
.
.
.
plot(b,r,'b');
legend(['qho-',num2str(i)]);
hold on
end
我该怎么做?
再见,
我还有其他问题: 如果我有以下内容
I have other question: if I have the following
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)
我只有最后一个数字的颜色图例.不能两者都用. 我该怎么解决?!
I got only color legend for the last figure only. not for both.. how can I solve that ?!
再次感谢
推荐答案
只需使用hold all
而不是hold on
并将图例标签放在单元格数组中
Just use hold all
instead of hold on
and put the legend labels in a cell array
hold all
for i=1:8
.
.
.
plot(b,r);
Leg{i} = ['qho-',num2str(i)];
end
legend(Leg)
例如,请参见以下问题:稀疏矩阵图matlab
See this question for example: Sparse matrix plot matlab
注意:
从 Matlab R2014b起,hold on
已被修改为类似于hold all
的行为,即,每次绘制时都更改图的颜色. 文档指出hold all
语法将在将来的版本中删除.
From Matlab R2014b onward, hold on
has been modified to act like hold all
, i.e. change the colours of the plots each time one is plotted. The docs state that the hold all
syntax will be removed in future releases.
这篇关于如何在MATLAB的每次迭代中更改图的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!