如何在matlab图中左右2个yaxis的比例,限制和刻度相同

How to make the same scale, limit and tick of 2 yaxis left and right in matlab plot

下面是我用来在 MATLAB 绘图中对两个 y 轴使用相同比例的代码:

%% Additions by Dev-iL:
date = 1:10;
z = 4*randn(3,10);
spread = 0.2*sum(z,1);
figure();
%% Original code by RSerrano:
ax(2) = subplot(2,1,2);
% z = horzcat(zscore,signal1,signal2); % Dev-iL
  yyaxis left
  plot(date,z,'LineWidth',0.5);
  ylabel('Z-score(residuals)');
  set(ax(2),'YColor',[0 0 0],'YDir','normal');
  ax(2).YLimMode = 'manual';
  ax(2).YLim = [-8 8];
  ax(2).YTickMode = 'manual';
  ax(2).YTick = -8:2:8;

co1 = get(gca,'ColorOrder'); 
% Change to new colors.
set(gca, 'ColorOrder', [0.83 0.82 0.78; 0 0.5 0; 0.47 0.67 0.19],...
         'NextPlot', 'replacechildren');
co1 = get(gca,'ColorOrder'); 
plot(date,z,'LineWidth',0.3);

z2 = spread;
yyaxis right
plot(date,z2,'Color',[0.31 0.31 0.31], 'LineWidth',0.5);
xlabel('Date');
ylabel('Spread(USD)');
title(['Spread and Trade Signals']);
legend('Z-score','Signal1', ...
'Signal2','Spread', ...
'Location','NE');
set(ax(2),'YColor',[0 0 0],'YDir','normal');

ax(2).YTick = -8:2:8;
axis tight
grid on

这导致:

如何让左y轴的ylimytick与右y轴相同?或者如何将左 y 轴的 ylimytick 应用到右 y 轴?

根据您使用的 yyaxis 判断,我假设您有 R2016a,因此使用 HG2。

作为 yyaxis 的替代方法,假设您只想在两边设置相同的刻度,您可以只复制坐标轴并将 y 轴的位置设置在右侧(如图所示)在类似的问题中 ):

hR = axes('ylim', [y(1) y(end)],'XTick', [], 'YTick', y,'YAxisLocation', 'right',...
            'XTickLabel',[]);

使用稍微重新排列的代码版本:

%% Definitions:
date = 1:10;
z = 4*randn(3,10);
z2 = 0.2*sum(z,1);
y = -8:2:8;
%% Create the figure:
figure(); hL = gca;
set(hL, 'ColorOrder', [0.83 0.82 0.78; 0 0.5 0; 0.47 0.67 0.19],...
        'NextPlot', 'replacechildren');
plot(date, z,'LineWidth',0.5); hold on;
plot(date, z2,'Color',[0.31 0.31 0.31], 'LineWidth',0.5);
%% Customize plots:
grid on
xlabel(hL,'Date');
ylabel(hL,'Z-score(residuals)');
hL.YLimMode = 'manual';
hL.YLim = [y(1) y(end)];
hL.YTickMode = 'manual';
hL.YTick = y;

title(['Spread and Trade Signals']);
legend(hL,'Z-score','Signal1', ...
'Signal2','Spread', ...
'Location','NE');

hR = axes('ylim', [y(1) y(end)],'XTick', [], 'YTick', y,'YAxisLocation', 'right',...
            'XTickLabel',[],'Color','none');
ylabel(hR,'Spread(USD)');
linkaxes([hL,hR],'xy'); % To sync zooming, panning etc.

结果如下:


subplot一起使用:

在这里,我们可能需要使用 copyobj 创建新轴,而不是使用 axes 创建新轴(发生这种情况是因为 axes 命令碰巧在正确 Position,这是轴的默认值 Position;在 subplot 中,Position 不是默认值,因此之前的技巧不起作用):

%% Definitions:
date = 1:10;
z = 4*randn(3,10);
z2 = 0.2*sum(z,1);
y = -8:2:8;
%% Create the figure:
figure(); subplot(2,1,2); hL = gca;
set(hL, 'ColorOrder', [0.83 0.82 0.78; 0 0.5 0; 0.47 0.67 0.19], 'NextPlot', 'replacechildren');
plot(date, z,'LineWidth',0.5); hold on;
plot(date, z2,'Color',[0.31 0.31 0.31], 'LineWidth',0.5);
%% Customize plots:
grid on
xlabel(hL,'Date');
ylabel(hL,'Z-score(residuals)');
hL.YLimMode = 'manual';
hL.YLim = [y(1) y(end)];
hL.YTickMode = 'manual';
hL.YTick = y;

hR = copyobj(hL,gcf);
hR.YAxisLocation = 'right';

title(['Spread and Trade Signals']);
legend(hL,'Z-score','Signal1', ...
'Signal2','Spread', ...
'Location','NE');

ylabel(hR,'Spread(USD)');
linkaxes([hL,hR],'xy'); % To sync zooming, panning etc.