0

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

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

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

在此处输入图像描述

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

4

3 回答 3

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');
于 2013-02-21T06:16:11.563 回答
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')
于 2013-02-21T06:14:57.140 回答
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').

于 2013-02-21T06:20:28.857 回答