matlab实现两个legend
时间: 2023-06-23 14:09:28 浏览: 85
在Matlab中,可以使用 `legend` 函数来为图表添加图例。如果你想添加两个图例,可以使用以下步骤:
1. 在绘制图表时,为每个数据系列都添加一个标签。例如:
```matlab
x = 1:10;
y1 = x.^2;
y2 = x.^3;
plot(x, y1, 'DisplayName', 'y = x^2');
hold on
plot(x, y2, 'DisplayName', 'y = x^3');
```
2. 在绘制完成后,使用 `legend` 函数来添加第一个图例。例如:
```matlab
legend('show');
```
3. 使用 `legend` 函数的 `Location` 参数来指定第二个图例的位置。例如:
```matlab
legend('Location', 'northeastoutside');
```
注意,第二个图例的标签需要手动添加。可以使用 `text` 函数来添加文本标签,或者使用 `annotation` 函数来添加注释标签。例如:
```matlab
text(11, 90, 'y = x^2', 'FontSize', 10);
annotation('arrow', [0.8 0.8], [0.6 0.7], 'String', 'y = x^3', 'FontSize', 10);
```
完整的代码如下:
```matlab
x = 1:10;
y1 = x.^2;
y2 = x.^3;
plot(x, y1, 'DisplayName', 'y = x^2');
hold on
plot(x, y2, 'DisplayName', 'y = x^3');
legend('show');
legend('Location', 'northeastoutside');
text(11, 90, 'y = x^2', 'FontSize', 10);
annotation('arrow', [0.8 0.8], [0.6 0.7], 'String', 'y = x^3', 'FontSize', 10);
```
这样就可以在同一个图表中添加两个图例了。
相关推荐
![](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)
![](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)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)