How to change axis limits and tick step of a MatLab figure?
Asked Answered
V

2

5

I have a simple plot of y against x.

y = [6,-1.3,-8,-11.7,-11,-6,1.3,8,11.7,11,6,-1.3];
x = 0:0.3:3.3;
plot (x,y)

As the result, the x-axis of the figure is ranging from 0 to 3.5, with scale of 0.5. I have used the XLimit = [0 3.3] to limit the axis, but it seems like not working.

I wish to make the x-axis range from 0 to 3.3 with steps of 0.3.

Volunteer answered 8/10, 2016 at 4:36 Comment(0)
E
6
axis tight % removes the empty space after 3.3
set(gca,'XTick',0:0.3:3.3) % sets the x axis ticks
Erland answered 8/10, 2016 at 4:55 Comment(1)
Thank you for your great effort!Volunteer
B
4

With XLimit = [0 3.3] you just define a vector called XLimit. In order to use this vector as horizontal limit, you should use xlim:

xlim(XLimit)
% or directly:
xlim([0, 3.3])

Read more about xlim here. Similarly you can set the vertical limit with ylim.

Since you are trying to set the limits equal to the range of x, you will probably find the following command most helpful:

axis tight

But note that it changes both x- and y-axis limits.

To set the tick step, as AVK said, you should set the 'XTick' to 0:0.3:3.3:

set(gca,'XTick',0:0.3:3.3)

gca is the handle to current axes.

Bebebebeerine answered 8/10, 2016 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.