scaling and change the ylim for subplot

I plotted a figure with two y-axis with different y limit
But I would like to limit the yyaxis of the right axis so that right axis starts with the lower limit of the left plot and upper limit of the right plot?
Or if it is possible that the zero-axis of both plots matches?
I manually shifted the right plotted axis so that the zeros matches for both axis.
Thank you very much for your inputs!!

 Accepted Answer

You should set ylim() for each axes separately. For example:
x = linspace (0, 6, 51)';
y1 = sin (x);
y2 = cos (x);
yyaxis ('left');
plot (x,y1);
ylim ([-2,2]);
yyaxis ('right');
plot (x,y2);
ylim ([-6,6]);

4 Comments

Hi Thanks for your sugggestion.
I tried plotting as you suggested, the result looks following:
I am uploading the data and the code I used
close all;clc;clear
load time.mat
load x_Bias.mat
load x_Sigma.mat
load y_Bias.mat
load y_Sigma.mat
ylabels={'X-axis bias','Y-axis bias','Z-axis bias'};
suptitle('Bias estimate versus Time')
AA = subplot(3,1,1);
% first plot
yyaxis(AA,'left')
plot(time,x_Bias,'r','Linewidth',1.5);
% second plot
hold on
yyaxis(AA,'right')
plot(time,x_Sig,'k--','Linewidth',2);
grid on;
ylabel(ylabels{1})
YLL = AA.YAxis(1).Limits %limits of left side
YLR = AA.YAxis(2).Limits %limits of right side
ylim ([-YLR(2),YLR(2)])
xlabel('Time [s]')
You should select bounds for ylim that makes their zeros match.
close all; clc; clear;
load time.mat
load x_Bias.mat
load x_Sigma.mat
load y_Bias.mat
load y_Sigma.mat
ylabels = {'X-axis bias','Y-axis bias','Z-axis bias'};
suptitle ('Bias estimate versus Time');
AA = subplot(3,1,1);
% first plot
yyaxis (AA,'left')
plot (time,x_Bias,'r','Linewidth',1.5);
ylim ([-0.02,0.04]);
% second plot
hold on
yyaxis (AA,'right')
plot (time,x_Sig,'k--','Linewidth',2);
hold off
ylim ([-2,4]);
grid on;
ylabel (ylabels{1});
% YLL = AA.YAxis(1).Limits; %limits of left side
% YLR = AA.YAxis(2).Limits; %limits of right side
% ylim ([-YLR(2),YLR(2)]) % do not set ylim again
xlabel ('Time [s]');
Ravindra
Ravindra on 7 Sep 2020
Edited: Ravindra on 7 Sep 2020
Thanks!!!
So every time I need to modify the ylim manually so that it passes with the left side axis. Is it possible to do it automatically?
ylim() only affects the current axis. When the command yyaxis(AA,'right') is executed, all of the future commands (including ylim()) affect the right yyaxis. You have to do this manually because the scale of your data for the left yyaxis and the right yyaxis differs significantly. The left yyaxis and the right yyaxis are separate and the ruler scale in one side does not affect the one on the other side.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!