Plot not displaying the y axis limits, even though "ylim" has been used.

I have used Ylim to set the Y-axis limit for my plot. It seems to be working for one end of the y axis, but not at the other end. Xan someone tell me what could be wrong with my plots here. I am attching the pic for reference and also the code that I am using.
figure()
yyaxis left
plot(rad,Br_3_fem_ss,'color',[1,0,0])
ylim([-0.6 0.6])
ylabel('Brad');
yyaxis right
plot(rad1,Cur_3_fem_lam_ss,'color',[0,0,1])
ylim([-5e5 5e5])
ylabel('Current density');
grid on
xlim([0 pi])
xticks(0:(pi/18):pi)
legend('Brad','current den','Location','SouthWest')
title('Plot for Brad at 3 for Laminated rotor');
xlabel('Angle (rad)');
set(gcf,'Color','w');
set(gca,'fontname','times');

 Accepted Answer

Try something like the following:
x = linspace(-1, 1, 201);
y1 = 0.48*sinpi(x);
y2 = -4e5*sinpi(x);
figure()
yyaxis left
plot(x, y1)
ylabel('Brad')
ylim([-0.6001 0.6001])
yyaxis right
plot(x, y2)
ylabel('Current density')
ylim([-5e5 5e5])
grid on

1 Comment

Thank you @Sam Chak, my issue got resolved by using theis method. I will try using yticks as suggested by @Walter Roberson and see if it works or not.
Thank you for helping.

Sign in to comment.

More Answers (1)

ylim is working for both sides.
However, the left side is not labeling the top end of the axes. You are not using yticks so there is no promise that the end of the range will happen to get labeled.

Asked:

on 24 Feb 2026 at 6:46

Commented:

on 24 Feb 2026 at 9:13

Community Treasure Hunt

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

Start Hunting!