matlab添加图例
时间: 2023-07-13 15:07:41 浏览: 72
在 MATLAB 中,可以使用 `legend` 函数添加图例。以下是一些添加图例的示例代码:
1. 基本用法:
```matlab
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, x, y2);
legend('sin', 'cos');
```
2. 自定义图例位置:
```matlab
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, x, y2);
legend('sin', 'cos', 'Location', 'northwest');
```
3. 添加图例标题:
```matlab
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, x, y2);
legend('sin', 'cos', 'Location', 'northwest', 'Title', 'Trigonometric Functions');
```
4. 自定义图例标记:
```matlab
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, '-o', x, y2, '--^');
legend('sin', 'cos', 'Location', 'northwest', 'Title', 'Trigonometric Functions');
```
在这个例子中,`-o` 和 `--^` 分别表示使用实心圆和虚线箭头来表示图例中的 `sin` 和 `cos`。
希望这些示例能够帮助你添加图例。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)