0

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?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
RSo
  • 17
  • 5
  • When posting example code, make sure it's at least runnable. See this for additional instruction: [mcve]. – Dev-iL Jun 16 '16 at 06:16
  • Not minding the example code that I posted(by the way, that's my personal code and it worked for me), would you know how to make the ylim of the right y axis similar to left y axis? I'm getting frustrated with MATLAB, it's not really user friendly. Many thanks. – RSo Jun 16 '16 at 06:47
  • The code is ok, but you have some undefined variables: `date`, `z` and `z2`. It would've been easy to make the above code runnable if you just added something like `date = 1:10; z = 10*randn(3,10); z2 = z;`. In any case I'm working on a solution for you. For now, take a look [here](http://stackoverflow.com/a/30381089/3372061). – Dev-iL Jun 16 '16 at 06:51
  • date, z and z2 are array. no need to run the code that I specified. or perhaps, create a value. What I am just asking is it possible to re-apply the ylim set from left y axis to right y axis? I will use the code for almost 20,000 matlab plot and it's very tedious if I will fix the scale of both left and right y axis of each plot one by one. I tried to use for left y axis the ylim(ax(2),[-10 10]) and use it again it right y axis but it didn't work. It seems like I need to fix the scale of each plot manually. Thanks – RSo Jun 16 '16 at 07:04
  • Rest assured - you won't have to do this manually. When you post code that allegedly causes some problem, it has to be runnable or else others can't reporoduce your problem and can't help you - simple as that. Note that this is not my personal opinion but the rules of SO. – Dev-iL Jun 16 '16 at 07:07
  • I took out the code I specified to have more focus on my question, since my question (I think) is generic anyway and no need to specify the code that I am using. – RSo Jun 16 '16 at 07:14
  • Nononono - it was critical that you had your code here, please put it back before somebody closes this question because it's now "unclear what you're asking". Either the code or at least a screenshot of what's wrong is something you should have. – Dev-iL Jun 16 '16 at 07:15
  • @RSerrano Could you add your original code back, please? It is very helpful for future readers to see your original example. – kkuilla Jun 16 '16 at 07:35

1 Answers1

1

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.
Community
  • 1
  • 1
Dev-iL
  • 23,742
  • 7
  • 57
  • 99