问题描述
我在使用legend
函数时遇到了一些问题.我的代码如下:
I'm having some issues with the legend
function. My code is as follows:
xax = logspace(1, 4, 1000);
R1 = sqrt(R11.*R21);
%freq and mag are vectors of length 300
loglog(freq, mag, 'k-');
hold on;
loglog(xax, R1, 'r-');
loglog(f1, R1, 'bo');
loglog(f2, R1, 'bo');
legend('|Zvc|', 'R1', 'f1', 'f2');
但是,图例无法正常运行.它在前两个显示黑线和红线,这很好.但是最后两个点显示为红线,而不是蓝色圆圈.这是一张显示错误图例的图片:
However, the legend doesn't work as I'd expect. It shows a black line and red line for the first two, which is fine. But the last two points are shown as red lines rather than blue circles. Here's a picture that shows the incorrect legend:
f1
和f2
是表示交点的标量值.
f1
and f2
are scalar values that indicate intersection points.
是否有一种方法可以调整我的代码,以使图例看起来正确?
Is there was a way to adjust my code so that the legend looks right?
推荐答案
legend
将最后两个图显示为红线的原因是您的第二个loglog
函数返回了多个句柄.看起来像是一行,但实际上是多行叠加.将loglog(xax, R1, 'r-');
更改为h=loglog(xax, R1, 'r-')
,您将看到. legend
函数按照创建顺序将您赋予它的字符串应用于当前绘图中的每个句柄.发生这种情况是因为R1
是标量,而xax
是矢量. Matlab的所有绘图功能都以这种方式工作.
The reason that legend
is showing the last two plots as red lines is that your second loglog
function is returning multiple handles. It looks like one line, but it's really multiple lines superimposed. Change loglog(xax, R1, 'r-');
to h=loglog(xax, R1, 'r-')
and you'll see. The legend
function applies the strings you give to it to each handle in the current plot in the order that that they were created. This happens because R1
is a scalar while xax
is a vector. All of Matlab's plotting functions work this way.
这是我要更改相关行的方法:
Here's how I would change the relevant line:
loglog(xax, R1+zeros(size(xax)), 'r-');
尽管始终是一行,但这足够了:
Though if it's always a line, this would suffice:
xax = logspace(1, 4, 2);
loglog(xax, [R1 R1], 'r-');
这篇关于Matlab图中单个点的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!