0

下面是我曾经在 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

这导致:

Dev-iL 的示例输出

如何使左 y 轴的ylimytick与右 y 轴相同?或者我如何将左 y 轴的ylimand应用于ytick右 y 轴?

4

1 回答 1

1

根据您使用的判断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

在这里,不是使用创建新轴,axes我们可能需要使用创建它们copyobj(发生这种情况是因为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.
于 2016-06-16T07:29:06.083 回答