在matlab中更改图例

时间:2013-02-21 06:06:08

标签: matlab figure

我有一些代码基本上可以做到这一点:

for i = 1:length(ReliabilityStruct)
    if (FailureFlags(i) == 0) 
        plot(X(i), Y(i), '.b');
    elseif (FailureFlags(i) == 1)
        plot(X(i), Y(i), 'or');
    elseif (FailureFlags(i) == 2)
        plot(X(i), Y(i), 'og');
    elseif (FailureFlags(i) == 3)
        plot(X(i), Y(i), 'ok');
    else
        fprintf('\nUnknown Flag, check Data\n')
        return
    end
end
drawnow;
legend('Recovery', '1', '2', '3');

所以我的目标是为不同的标志制作一个具有不同符号的图形。见下文:

enter image description here

如您所见,Legend并不完全符合数据。如何更改每个图例条目以解决此问题?或者,有没有更好的方法来解决这个问题?

3 个答案:

答案 0 :(得分:3)

我认为你可以使用这样的东西(额外的好处是你避免循环!):

ind = FailureFlags==0;
plot(X(ind), Y(ind), '.b');

ind = FailureFlags==1;
plot(X(ind), Y(ind), 'or');

ind = FailureFlags==2;
plot(X(ind), Y(ind), 'og');

ind = FailureFlags==3;
plot(X(ind), Y(ind), 'ok');

legend('Recovery', '1', '2', '3');

答案 1 :(得分:1)

试一试。在循环中为每个绘图创建一个赋值如下:

 p1=plot(X(i), Y(i), '.b');
elseif (FailureFlags(i) == 1)
 p2=plot(X(i), Y(i), 'or');
elseif (FailureFlags(i) == 2)
 p3=plot(X(i), Y(i), 'og');
elseif (FailureFlags(i) == 3)
 p4=plot(X(i), Y(i), 'ok');

然后你可以使用图例来表示具体的事情:

legend([p1 p2],'stuff','morestuff')

答案 2 :(得分:1)

请记住,您可以使用help命令参考任何函数的用法。对于您的情况,help legend将为您提供如下的使用示例。

legend(H,string1,string2,string3, ...) puts a legend on the plot
containing the handles in the vector H using the specified strings as
labels for the corresponding handles.

因此,您可以通过将绘图分配给变量(例如p1=plot(X(i), Y(i), '.b');)来获取绘图处理程序。然后通过使用处理程序作为第一个参数调用命令来绘制图例,例如legend([p1], 'something')

相关问题