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

2k views Asked by At

Below is the code that I used to have the same scale for both y axis in MATLAB plot:

%% 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

This results in:

Example output by Dev-iL

How I can make the ylim and ytick of left y axis the same with right y axis? or how can I apply the ylim and ytick of left y axis to the right y axis?

1

There are 1 answers

2
Dev-iL On BEST ANSWER

Judging by the yyaxis you're using, I'd assume you have R2016a and therefore using HG2.

As an alternative to yyaxis, assuming you just want to have the same ticks on both sides, you can just copy the axes and set the position of the y axis to be on the right (as demonstrated in a similar problem here):

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

Using a slightly rearranged version of your code:

%% 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.

Here's the result:

enter image description here


To use with subplot:

Here, instead of creating new axes using axes we might need to create them using copyobj instead (this happens because the axes command happened to create the new axes in the correct Position, which is the default Position for axes; in subplot the Position is not default so the previous trick doesn't work):

%% 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.