This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Controlling Axis Location

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);


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 = 
ans = 

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 top, bottom, or origin. You can set YAxisLocation to either left, right, or origin.

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';

Was this topic helpful?