Bazman
Bazman

Reputation: 2150

How to change limits of y-axis? `ylim` does not work

When the graph below is plotted, NSS1 which is simply a constant set equal to one is right on the top border of the graph and thus hard to see.

How can I change the length of the y-axis to say 1.2 so that the NSS1 can be seen more clearly?

lambda=5;
tau=0:30;

tau(1)=0.000001;

NSS1=1*ones(1,31);
NSS2=(1-exp(-tau/lambda))./(tau/lambda);
NSS3=((1-exp(-tau/lambda))./(tau/lambda)-exp(-tau/lambda));

%ylim([0, 1.2])
plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');
xlabel('t = 0 to 30y', 'FontSize',30)
ylabel('yield','FontSize',30)

Upvotes: 4

Views: 14566

Answers (1)

Robert Seifert
Robert Seifert

Reputation: 25232

The reason why ylim doesn't work if you put it before the plot command is that there is no axes object it can relate to.

So there are two options:

First, you create an axes object and hold it with hold on, so the upcoming plot is plotted on the same axis.

ax = axes; hold on;
ylim([0, 1.2])

plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');

or second, you plot first, the command automatically generates an axes object and you can modify its y-limits afterwards:

plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');
ylim([0, 1.2])

Upvotes: 3

Related Questions