This example shows how to change the location of an axis in MATLAB® to customize your graph. By adjusting the location of an axis, you can display axis lines through the origin.
Create a line plot. By default, the x-axis is at the bottom of the graph and the y-axis is on the left side.
x = -2*pi:0.1:2*pi; y = sin(x); plot(x,y) xlabel('x') ylabel('sin(x)')
The gca function returns the current axes for the current figure. Like all graphics objects, axes objects have properties that you can view and modify.
ax = gca;
Query the current location of the x and y-axis.
ans = 'bottom'
ans = 'left'
You can use these properties to change the position of an axis.
Set the x-axis so it passes through the origin of the y-axis (y = 0).
ax.XAxisLocation = 'origin';
Set the y-axis so it passes through the origin of the x-axis (x = 0).
ax.YAxisLocation = 'origin'; % setting y axis location to origin
You can choose other values for the axis location. You set XAxisLocation to either
origin. You can set YAxisLocation to either
Turn off the display of the axes outline by setting the Box property. Use the Axes Layer property to make grid lines and tick marks appear over graphics objects.
ax.Box = 'off'; ax.Layer = 'top';