如何在Matlab图中添加2列图例?

Kar*_*rlo 9 matlab plot graph legend legend-properties

考虑以下代码:

t=0:.01:(2*pi);
y=[sin(t);sin(t-pi/12);sin(t-pi/6);sin(t-pi/4)];
figure(1)
    clf
    subplot(6,1,5)
    plot(t,y)
    xlim([0 2*pi])
    legend('1','2','3','4')
Run Code Online (Sandbox Code Playgroud)

它产生以下数字:

![在此处输入图像说明

有没有办法将图例更改为2列布局?所以它会

--- 1 --- 3

--- 2 --- 4

代替

--- 1

--- 2

--- 3

--- 4

因此,图例边界线不会越过图形边界线.

我找到了gridLegend脚本,但我更喜欢直接编写代码.

小智 5

MATLAB 在2018a 版本中引入了对图例中多列的本机支持。只需'NumColumns',desired_numberlegend()命令末尾添加即可。

在此处查看详细信息 - https://www.mathworks.com/help/matlab/ref/legend.html?lang=en&s_tid=gn_loc_drop#bt6r30y

此外,图例条目的方向可以从上到下更改为从左到右。

默认情况下,图例沿每列从上到下对项目进行排序。要沿每行从左到右排列项目,请将 Orientation 属性设置为 'horizo​​ntal'。