一、方法一yyaxis函数
x = linspace(0,pi);
y1 = cos(x);
yyaxis left; % 激活左边的轴
plot(x,y1)
xlabel('X-axis');
ylabel('left Y-axis'); % 给左y轴添加轴标签
hold on
yyaxis right; % 激活右边的轴
y2 = cos(2*x);
plot(x,y2)
ylim([-1,1]); % 设置右y轴的界限
ylabel('right Y-axis'); % 给右y轴添加轴标签
![Matlab绘制双纵轴图(yyaxis函数)_第1张图片](http://img.e-com-net.com/image/info8/66d0cf576dfc43c2908a51dbfd2d5b36.jpg)
二、更改坐标轴颜色
x = linspace(0,pi);
y1 = cos(x);
yyaxis left; % 激活左边的轴
plot(x,y1,'r')
xlabel('X-axis');
ylabel('left Y-axis'); % 给左y轴添加轴标签
ax = gca;
ax.YColor = 'r';
hold on
yyaxis right; % 激活右边的轴
y2 = cos(2*x);
plot(x,y2,'b')
ylim([-1,1]); % 设置右y轴的界限
ylabel('right Y-axis'); % 给右y轴添加轴标签
ax = gca;
ax.YColor = 'b';
![Matlab绘制双纵轴图(yyaxis函数)_第2张图片](http://img.e-com-net.com/image/info8/a197a6bd8c4c4e839d5b1b5697e17412.jpg)
三、方法二plotyy函数
x = linspace(0,pi);
y1 = cos(x);
y2 = cos(2*x);
[AX,H1,H2] =plotyy(x,y1, x,y2); % 绘制双轴图
HH1=get(AX(1),'Ylabel');
set(HH1,'String','Left Y-axis');
HH2=get(AX(2),'Ylabel');
set(HH2,'String','Right Y-axis');
![Matlab绘制双纵轴图(yyaxis函数)_第3张图片](http://img.e-com-net.com/image/info8/adfd1c6ecdb84542b170992167fa0ddc.jpg)
四、更改坐标轴颜色
x = linspace(0,pi);
y1 = cos(x);
y2 = cos(2*x);
[AX,H1,H2] =plotyy(x,y1, x,y2); % 绘制双轴图
HH1=get(AX(1),'Ylabel');
set(HH1,'String','Left Y-axis');
HH2=get(AX(2),'Ylabel');
set(HH2,'String','Right Y-axis');
xlabel('X');
set(AX(1),'XColor','k','YColor','b');
set(AX(2),'XColor','k','YColor','r');
set(H1,'color','b');
set(H2,'color','r');
set(HH1,'color','b');
set(HH2,'color','r');
![Matlab绘制双纵轴图(yyaxis函数)_第4张图片](http://img.e-com-net.com/image/info8/22b7b2dcc16d4161badf45eeccf2e872.jpg)