【问题标题】:Change Legend diagram in matlab在matlab中更改图例图
【发布时间】:2013-02-21 06:06:07
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: matlab figure


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      试试这个。在您的循环中为每个图分配如下:

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

      【讨论】:

        【解决方案3】:

        请记住,您可以使用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')

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-05-31
          • 1970-01-01
          • 2016-12-30
          • 1970-01-01
          • 2017-08-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多