用matlab在同一张图上,绘制三个不同颜色的信号,并添加图例。x轴是时间(以秒为单位),y轴是振幅。
时间: 2024-05-29 10:12:44 浏览: 8
假设三个信号分别为x1、x2和x3,对应时间序列为t。
可以按照以下步骤绘制图像并添加图例:
1. 创建一个新的figure窗口:
```matlab
figure;
```
2. 绘制三个信号的曲线,分别用不同颜色表示。可以使用plot函数,同时指定线条的样式和颜色:
```matlab
plot(t, x1, 'r-', t, x2, 'g--', t, x3, 'b-.');
```
这里,'r-'表示红色实线,'g--'表示绿色虚线,'b-.'表示蓝色点划线。
3. 添加图例。可以使用legend函数,指定每条曲线对应的标签:
```matlab
legend('Signal 1', 'Signal 2', 'Signal 3');
```
这里,'Signal 1'、'Signal 2'和'Signal 3'分别为三个信号的标签。
完整的代码如下:
```matlab
% 生成时间序列t和三个信号x1、x2和x3
t = linspace(0, 10, 1000);
x1 = sin(2*pi*t);
x2 = cos(2*pi*t);
x3 = 0.5*sin(4*pi*t);
% 创建新的figure窗口
figure;
% 绘制三个信号的曲线,分别用不同颜色表示
plot(t, x1, 'r-', t, x2, 'g--', t, x3, 'b-.');
% 添加图例
legend('Signal 1', 'Signal 2', 'Signal 3');
% 设定x轴和y轴标签
xlabel('Time (s)');
ylabel('Amplitude');
```
运行以上代码,将会得到一张包含三个信号的图像,如下所示:
![三个信号的图像](https://i.loli.net/2021/04/20/7gK5dLxNj2QbZfO.png)
相关推荐
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)