【问题标题】:How do I add two legends to a single plot in MATLAB?如何在 MATLAB 中将两个图例添加到单个图中?
【发布时间】:2012-06-30 12:27:12
【问题描述】:

我想在 MATLAB 的绘图中添加两个图例。我该怎么做?

【问题讨论】:

    标签: matlab plot legend matlab-figure


    【解决方案1】:

    您可以创建第二个叠加轴,带有自己的图例(当然在不同的位置)。


    编辑:

    这是一个简单的例子:

    %# create some plot with a legend
    hAx(1) = axes();
    hLine(1) = plot(1:10, 'Parent',hAx(1));
    set(hAx(1), 'Box','off')
    legend(hLine(1), 'line')
    
    %# copy the axis
    hAx(2) = copyobj(hAx(1),gcf);
    delete( get(hAx(2),'Children') )            %# delete its children
    hLine(2) = plot(sin(1:10), 'Color','r', 'Parent',hAx(2));
    set(hAx(2), 'Color','none', 'XTick',[], ...
        'YAxisLocation','right', 'Box','off')   %# make it transparent
    legend(hLine(2), {'curve'}, 'Location','NorthWest', 'Color','w')
    

    【讨论】:

      【解决方案2】:

      要创建粘性图例,您可以致电copyobj

      handle_legend = legend(handle_plot, 'string1');
      copyobj(handle_legend, handle_figure);
      

      copyobj 函数只是将其关联的图例保留在图中。

      这适用于单个轴(无需创建第二个叠加轴),并且可以通过这种方式添加多个图例。

      例子:

      %declare figure
      hfigure = figure('Color', 'w');
      
      %plot 2 lines (red and blue)
      hplot1 = plot(1:10,'r-.x');
      hold on;
      hplot2 = plot(10:-1:1,'b--o');
      
      %plot legends
      hlegend1 = legend(hplot1, 'Data 1', 'Location','East'); %display legend 1
      new_handle = copyobj(hlegend1,hfigure);                 %copy legend 1 --> retain
      legend(hplot2, 'Data 2', 'Location','West');            %display legend 2
      

      【讨论】:

      • 此方法不适用于我 (R2016a),即使使用 'legacy' 作为选项。
      【解决方案3】:

      制作第一个图例后,制作一个新的、不可见的轴手柄:

      ax=axes('Position',get(gca,'Position'),'Visible','Off');
      

      现在在新轴上制作第二个图例:

      legend(ax,...);
      

      这与@Amro 的回答基本相同,但更简单、更短。

      【讨论】:

      • 我在 R2016b 中尝试了这个但没有成功:Plot1;调整 xticks 和 yticks;新轴; ...;新的刻度和标签与旧的重叠。
      【解决方案4】:

      多图示例:

      hAx(1) = axes();
      hold on
      hLine(1) = plot(1:10, 'Parent',hAx(1),'color','b');
      hLine(2) = plot(3:15, 'Parent',hAx(1),'color','b', 'linestyle','--');
      set(hAx(1), 'Box','off')
      legend([hLine(1), hLine(2)],{ 'line' 'line2'})
      
      %# copy the axis
      hAx(2) = copyobj(hAx(1),gcf);
      delete( get(hAx(2),'Children') )            %# delete its children
      hold on
      hLine(3) = plot(sin(1:10), 'Color','r','Parent',hAx(2));
      hLine(4) = plot(cos(1:10), 'Color','r','linestyle','--','Parent',hAx(2));
      hold off
      set(hAx(2), 'Color','none', 'XTick',[], ...
      'YAxisLocation','right', 'Box','off')   %# make it transparent
      legend([hLine(3),hLine(4)], {'sin' , 'cos'}, 'Location','NorthWest', 'Color','w')
      %legend(hLine(3), {'sin'}, 'Location','NorthWest', 'Color','w')
      

      【讨论】:

      • 完全不明显,但这是我需要的八度音阶,谢谢:legend([hLine(3),hLine(4)], {'sin' , 'cos'})
      猜你喜欢
      • 2015-08-05
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多