matlab双y轴绘图代码
时间: 2024-08-14 22:00:34 浏览: 68
在MATLAB中,绘制双Y轴图表通常需要利用`yyaxis`函数来设置两个独立的Y轴。下面是一个简单的示例代码:
```matlab
% 创建数据
x = linspace(0, 10, 100);
y1 = sin(x);
y2 = cos(x);
% 创建基本图形
figure
plot(x, y1, 'b', 'LineWidth', 2) % 主Y轴,蓝色线
hold on % 保持当前坐标系以便添加更多曲线
% 添加第二个Y轴并设置范围
yyaxis right % 在右侧添加新的Y轴
plot(x, y2, 'r', 'LineWidth', 2) % 红色线,对应副Y轴
ylim([0 1]) % 设置副Y轴的上下限
% 添加标题和标签
title('Double Y-Axis Plot')
xlabel('X-axis')
ylabel('Main Y-axis (sin(x))') % 主Y轴标签
ylabel('Right Y-axis (cos(x))', 'Color', 'r') % 副Y轴标签,颜色设置为红色
% 显示网格
grid on
% 提示用户如何调整两个Y轴的刻度
text(5, 1.1, 'Drag the lower red line to adjust the secondary axis scale', 'Color', 'r')
% 提交绘图
hold off
```
这个例子中,我们首先创建了两个正弦和余弦函数的数据,然后在一个图窗口里分别用主Y轴画出正弦函数,用右Y轴画出余弦函数。你可以根据实际数据和需求修改这个代码。
阅读全文
相关推荐



















