matlab legend换行 多个legend 阵列排布

目标:把4个图例弄成2X2的出现在图中

在网上找了2010年之前的不少参考问答,都稀里糊涂没有实现还很麻烦
后来看了legend函数官方解释,发现本来就有这个功能,搬运一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
x = linspace(0,pi);
y1 = cos(x);
plot(x,y1)

hold on
y2 = cos(2*x);
plot(x,y2)

y3 = cos(3*x);
plot(x,y3)

y4 = cos(4*x);
plot(x,y4)
hold off

legend({'cos(x)','cos(2x)','cos(3x)','cos(4x)'},'Location','northwest','NumColumns',2)


自带函数他不香吗