一、方法一yyaxis函数
bash
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轴添加轴标签
二、更改坐标轴颜色
bash
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';
三、方法二plotyy函数
bash
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');
四、更改坐标轴颜色
bash
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');