MATLAB中legend用法小细节

 大家在用legend添加图例时有没有遇到过如下问题呢

“不支持使用整数来指定图例位置。请使用 'Location' 参数来指定图例相对于坐标轴的位置。”

根据之前的学习,是用数字参数来确定注释视窗在图形中的位置的,但是在2016b这个版本会出错(可能是高版本的原因),原来的参数集如下:

    参数字符串                                      含  义  

0                              尽量不与数据冲突,自动放置在最佳位置  

1                                                          放置在图形的右上角 

2                                                          放置在图形的左上角    

3                                                          放置在图形的左下角

4                                                          放置在图形的右下角    

-1                              放置在图形视窗的外右边    

查询帮助找到以下参数集:

Value

Description

'north'

Inside top of axes

'south'

Inside bottom of axes

'east'

Inside right of axes

'west'

Inside left of axes

'northeast'

Inside top-right of axes (default for 2-D axes)

'northwest'

Inside top-left of axes

'southeast'

Inside bottom-right of axes

'southwest'

Inside bottom-left of axes

'northoutside'

Above the axes

'southoutside'

Below the axes

'eastoutside'

To the right of the axes

'westoutside'

To the left of the axes

'northeastoutside'

Outside top-right corner of the axes (default for 3-D axes)

'northwestoutside'

Outside top-left corner of the axes

'southeastoutside'

Outside bottom-right corner of the axes

'southwestoutside'

Outside bottom-left corner of the axes

'best'

Inside axes where least conflict occurs with plot data

'bestoutside'

To the right of the axes

'none'

Determined by Position property. Use the Position property to display the legend in a custom location.

 有些小伙伴是不是直接复制过去了呢,哈哈,我当时也是这么做的。是会报错的哦!

大家在使用这些参数集之前记得声明一下'Location'哦,举个栗子(开头大写的Location,开头小写的northwest哦):

legend('实测值','预测值','Location','northwest')

这是对两个曲线的图形及进行图例注释。

在legend的帮助里还找到了下面的例子:

x = -pi:pi/20:pi;
           y1 = sin(x);
           plot(x,y1)

hold on
           y2 = cos(x);
           plot(x,y2,'--')
           hold off

legend('sin(x)','cos(x)','Location','northwest','Orientation','horizontal')

这个例程的结果如下图:

大家实践一下就会知道Orientation的神奇作用了~

猜你喜欢

转载自blog.csdn.net/ZC_lianshuang/article/details/81385672