赞
踩
- y1 = sin(x);
- y2 = 3*sin(x);
- y3 = cos(x);
- y4 = 3*cos(x);
例子:如果我们有4条曲线,分布是cos类和sin类,我们的图例就想区分是cos类还是sin类。
我们要把我们所需要的类别曲线先画一遍,
比如说,我们上面有红色和绿色,那么我们就先绘制一条红色和绿色的曲线
- figure;
- hold on;
- plot(x, y1, 'r',DisplayName='sin(x)'); % 第一步
- plot(x,y3,'g',DisplayName='cos(x)');
将剩下的曲线绘制上去,颜色随便选,可以选绿,也可以选红。
- figure;
- hold on;
- plot(x, y1, 'r',DisplayName='sin(x)'); % 第一步
- plot(x,y3,'g',DisplayName='cos(x)');
-
- plot(x, y2, 'r'); % 第二步
- plot(x,y4,'g');
在图例中加入我们的线条的名称,有几类就有几条,图例的顺序可以随意
- x = linspace(0, 2.3*pi, 1000);
- y1 = sin(x);
- y2 = 3*sin(x);
- y3 = cos(x);
- y4 = 3*cos(x);
- figure;
- hold on;
- plot(x, y1, 'r',DisplayName='sin(x)'); % 第一步
- plot(x,y3,'g',DisplayName='cos(x)');
-
- plot(x, y2, 'r'); % 第二步
- plot(x,y4,'g');
-
- legend('cos(x)','sin(x)'); % 第三步
- hold off;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。