Matlab图例颜色不符合直线 [英] Matlab legend colors don't match lines

查看:256
本文介绍了Matlab图例颜色不符合直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道这里发生了什么问题。我在下面创建了一个小例子:

  clear 
steps = 1:6;
labels = cell(length(steps),1);
xvals = 1:10;
fig = figure(1);
ax =轴('parent',fig);
持有
,ii = 1:长度(步数)
s =步数(ii);
yvals =零(长度(xvals))+ ii;
labels {ii} = ['gain ='num2str(s)];
plot(ax,xvals,yvals);
end
legend(ax,labels);
延期

结果在我的系统中:





如果线条较少,甚至可以将图例中的颜色置入不均匀在剧情上。发生了什么?!

解决方案

解释发生了什么



问题在线


yvals = zeros(长度(xvals))+ ii;

这会创建一个10x10方形矩阵,而不是1x10矢量。 p>

但该矩阵的所有列都是相同的,因此每个列都覆盖(覆盖)前一个列表,并且只能看到 last 颜色。 p>

现在,颜色循环有7种颜色,矩阵有10列。因此,在第一次迭代中,最后一个绘制的列(您看到的那一列)具有颜色 mod(10,7)== 3 (黄色)。在第二次迭代中,你从 3 开始循环10次以上的颜色,也就是你得到颜色 mod(3 + 10,7)== 6 (浅蓝色)。等等。因此,您在图中看到的颜色取决于循环索引 ii ,但不符合您的预期。


Not sure what is going wrong here. I created a minimal example below:

clear
steps = 1:6;
labels = cell(length(steps),1);
xvals = 1:10;
fig = figure(1);
ax = axes('parent',fig);
hold on
for ii=1:length(steps)
    s=steps(ii);
    yvals = zeros(length(xvals)) + ii;
    labels{ii} = ['gain = ' num2str(s)];
    plot(ax,xvals,yvals);
end
legend(ax, labels);
hold off

And the result on my system is:

With less lines it can even put colors in the legend that aren't even on the plot. What is happening?!

解决方案

Explanation of what's happening

The problem is in the line

yvals = zeros(length(xvals)) + ii;

This creates a 10x10 square matrix, not a 1x10 vector. plot then plots each column of that matrix against xvals. That causes a mixing up of colors which is probably not what you want.

It's interesting to analyze specifically what happens. Matlab plots each column of that matrix with a different color, using the default cycle of colors, which in Matlab R2014b onwards is

But all columns of that matrix are the same, so each covers (overwrites) the preceding one, and you only see the last color.

Now, the color cycle has 7 colors and the matrix has 10 columns. So in the first iteration the last plotted column (the one you see) has color mod(10,7)==3 (yellow). In the second iteration you cycle through 10 more colors starting from 3, that is, you get color mod(3+10,7)==6 (light blue). And so on. Thus the color you see in the figure depends on the loop index ii, but not in the way you expected.

legend creates its entries by picking the color (and line spec) of each plotted line, in the order in which they were plotted. There are 10*6==60 plotted lines, each corresponding to a column of a matrix. But since you only supply six strings to legend, it just picks the first six of those lines, and uses their colors to create the legend entries. Those colors follow the default order, as explained above.

None of those first six lines that make it into the legend are actually seen in the figure, because they are covered by other lines. But legend doesn't care about that. So you get six legend entries with the default color order, which of course doesn't match the lines you actually see in the graph.

Solution

From the above, the solution is clear: replaced the referred line by

yvals = zeros(1, length(xvals)) + ii;

to create yvals as a vector (not a matrix). That way you'll get the figure you expected:

这篇关于Matlab图例颜色不符合直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆