【 MATLAB 】legend 的使用简析

举报
李锐博恩 发表于 2021/07/15 06:15:50 2021/07/15
【摘要】 目录 legend 在作图命令中(plot)给出图例标签; legend(label1,...,labelN) 给当前轴添加图例; legend(target,___) 给特定轴添加图例; legend(subset,___) 给部分函数添加图例; legend(___,'Location',lcn) 指定图例的位置(方向)以及显示的列数; lgd&nb...

目录

legend

在作图命令中(plot)给出图例标签;

legend(label1,...,labelN)

给当前轴添加图例;

legend(target,___)

给特定轴添加图例;

legend(subset,___)

给部分函数添加图例;

legend(___,'Location',lcn)

指定图例的位置(方向)以及显示的列数;

lgd = legend(___)

给图例添加标题;

legend('boxoff')

去除图例边框;

Modify Legend Appearance

修改图例的外观;


MATLAB 中 legend 可以翻译为图例,下面逐一用例子来说明 legend 的用法,并辅以必要的说明。

我只挑选几种使用的来说明。


legend

在作图命令中(plot)给出图例标签;

Plot two lines. Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text. Then, add a legend.

画两条线。 通过将DisplayName属性设置为所需文本,在绘图命令期间指定图例标签。 然后,添加一个图例(legend)。


  
  1. % Plot two lines.
  2. % Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text.
  3. % Then, add a legend.
  4. clear
  5. clc
  6. close all
  7. x = linspace(0,pi);
  8. y1 = cos(x);
  9. plot(x,y1,'DisplayName','cos(x)')
  10. hold on
  11. y2 = cos(2*x);
  12. plot(x,y2,'DisplayName','cos(2x)')
  13. hold off
  14. legend

对代码做如下改动,将两幅图分开画,然后添加图例的方法:


  
  1. % Plot two lines.
  2. % Specify the legend labels during the plotting commands by setting the DisplayName property to the desired text.
  3. % Then, add a legend.
  4. clear
  5. clc
  6. close all
  7. x = linspace(0,pi);
  8. y1 = cos(x);
  9. subplot(2,1,1);
  10. plot(x,y1,'DisplayName','cos(x)')
  11. legend
  12. %hold on
  13. y2 = cos(2*x);
  14. subplot(2,1,2);
  15. plot(x,y2,'DisplayName','cos(2x)')
  16. % hold off
  17. legend


legend(label1,...,labelN)

给当前轴添加图例;

legend(label1,...,labelN) sets the legend labels. Specify the labels as a list of character vectors or strings, such as legend('Jan','Feb','Mar').

legend(label1,...,labelN)设置图例标签。 将标签指定为字符向量或字符串列表,例如legend('Jan','Feb','Mar')。

绘制两条线并在当前轴上添加图例。 将图例标签指定为图例功能的输入参数。


  
  1. % Plot two lines and add a legend to the current axes.
  2. % Specify the legend labels as input arguments to the legend function.
  3. clear
  4. clc
  5. close all
  6. x = linspace(0,pi);
  7. y1 = cos(x);
  8. plot(x,y1)
  9. hold on
  10. y2 = cos(2*x);
  11. plot(x,y2)
  12. legend('cos(x)','cos(2x)')

将程序稍作改动,将这两个正弦函数的图像画在不同的子图中,并添加标签:


  
  1. % Plot two lines and add a legend to the current axes.
  2. % Specify the legend labels as input arguments to the legend function.
  3. clear
  4. clc
  5. close all
  6. x = linspace(0,pi);
  7. y1 = cos(x);
  8. subplot(2,1,1);
  9. plot(x,y1)
  10. legend('cos(x)')
  11. % hold on
  12. y2 = cos(2*x);
  13. subplot(2,1,2);
  14. plot(x,y2)
  15. legend('cos(2x)')


legend(target,___)

给特定轴添加图例;


  
  1. % Create a figure with two subplots and return the two Axes objects, ax1 and ax2.
  2. % Plot random data in each subplot. Add a legend to the upper subplot by specifying ax1 as the first input argument to legend.
  3. clear
  4. clc
  5. close all
  6. x = linspace(0,pi);
  7. y1 = rand(3);
  8. ax1 = subplot(2,1,1);
  9. plot(y1)
  10. y2 = rand(5);
  11. ax2 = subplot(2,1,2);
  12. plot(y2)
  13. legend(ax1,{'Line 1','Line 2','Line 3'})


legend(subset,___)

给部分函数添加图例;

subset 是子集的意思,它表示只给这个子集中的函数添加图例,后面的——表示,这些图例的标签,即它们的名字是什么?

下面给出一个实例,参考起来很容易理解:


  
  1. % If you do not want to include all of the plotted graphics objects in the legend,
  2. % then you can specify the graphics objects that you want to include.
  3. % Plot three lines and return the Line objects created.
  4. % Create a legend that includes only two of the lines.
  5. % Specify the first input argument as a vector of the Line objects to include.
  6. clear
  7. clc
  8. close all
  9. x = linspace(0,pi);
  10. y1 = cos(x);
  11. p1 = plot(x,y1);
  12. hold on
  13. y2 = cos(2*x);
  14. p2 = plot(x,y2);
  15. y3 = cos(3*x);
  16. p3 = plot(x,y3);
  17. hold off
  18. legend([p1 p3],{'cos(x)','cos(3x)'})


legend(___,'Location',lcn)

指定图例的位置(方向)以及显示的列数;


  
  1. % Plot four lines. Create a legend in the northwest area of the axes.
  2. % Specify the number of legend columns using the NumColumns property.
  3. clear
  4. clc
  5. close all
  6. x = linspace(0,pi);
  7. y1 = cos(x);
  8. plot(x,y1)
  9. hold on
  10. y2 = cos(2*x);
  11. plot(x,y2)
  12. y3 = cos(3*x);
  13. plot(x,y3)
  14. y4 = cos(4*x);
  15. plot(x,y4)
  16. hold off
  17. legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},'Location','northwest','NumColumns',2)


lgd = legend(___)

给图例添加标题;


  
  1. % Plot two lines and create a legend. Then, add a title to the legend.
  2. clear
  3. clc
  4. close all
  5. x = linspace(0,pi);
  6. y1 = cos(x);
  7. plot(x,y1)
  8. hold on
  9. y2 = cos(2*x);
  10. plot(x,y2)
  11. hold off
  12. lgd = legend('cos(x)','cos(2x)');
  13. title(lgd,'My Legend Title')


legend('boxoff')

去除图例边框;


  
  1. % Plot two lines and create a legend in the lower left corner of the axes.
  2. % Then, remove the legend background and outline.
  3. clear
  4. clc
  5. close all
  6. x = linspace(0,pi);
  7. y1 = cos(x);
  8. plot(x,y1)
  9. hold on
  10. y2 = cos(2*x);
  11. plot(x,y2)
  12. hold off
  13. legend({'cos(x)','cos(2x)'},'Location','southwest')
  14. legend('boxoff')


Modify Legend Appearance

修改图例的外观;


  
  1. % Modify the legend appearance by setting Legend properties.
  2. clear
  3. clc
  4. close all
  5. rdm = rand(4);
  6. plot(rdm)
  7. lgd = legend({'Line 1','Line 2','Line 3','Line 4'},'FontSize',12,'TextColor','blue')

当然也可以通过下面的方式来修改:


  
  1. % Modify the legend appearance by setting Legend properties.
  2. clear
  3. clc
  4. close all
  5. rdm = rand(4);
  6. plot(rdm)
  7. lgd = legend('Line 1','Line 2','Line 3','Line 4');
  8. lgd.FontSize = 12;
  9. lgd.TextColor = 'blue';

效果是一样的。

下面多改几下:


  
  1. % Modify the legend appearance by setting Legend properties.
  2. clear
  3. clc
  4. close all
  5. rdm = rand(4);
  6. plot(rdm)
  7. lgd = legend('Line 1','Line 2','Line 3','Line 4');
  8. lgd.FontSize = 12;
  9. lgd.TextColor = 'blue';
  10. lgd.NumColumns = 2;
  11. lgd.Location = 'southwest';
  12. leg.Orientation = 'vertical';
  13. title(lgd,'My Legend Title');

 

 

 

文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。

原文链接:reborn.blog.csdn.net/article/details/82857626

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。