Legend for a single point in Matlab plot

荒凉一梦 提交于 2019-12-10 21:46:09

问题


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 and f2 are scalar values that indicate intersection points.

Is there was a way to adjust my code so that the legend looks right?


回答1:


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-');


来源:https://stackoverflow.com/questions/19894638/legend-for-a-single-point-in-matlab-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!