Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# grid

Grid lines for 2-D and 3-D plots

## Syntax

grid on
grid off
grid
grid minor
grid(axes_handle,...)

## Description

grid on adds major grid lines to the current axes.

grid off removes all grid lines from the current axes.

grid toggles the visibility of the major grid lines of the current axes.

grid minor toggles the visibility of the minor grid lines of the current axes.

grid(axes_handle,...) uses the axes specified by axes_handle instead of the current axes.

## Examples

Use the grid function to display the major grid lines for a sine plot. By default, MATLAB® does not display grid lines for a 2-D view.

```figure
x = linspace(0,10);
y = sin(x);
plot(x,y);
grid on;```

Reissuing the command grid toggles the visibility of the major grid lines.

Create a 3-D plot and turn off the display of the grid lines. By default, MATLAB displays grid lines for a 3-D view.

```figure
surf(peaks);
grid off;```

Use the grid function to display the major and minor grid lines.

Set up a vector of random numbers.

```rng(0,'twister');
randnumbers = rand(1,20);```

Create a figure and get the handle to the current axes. Display the grid lines and set the line style for the major grid lines using the axes handle.

```figure
axh = gca;
plot(randnumbers);
grid on;
set(axh,'GridLineStyle','-');
grid minor;```

expand all

### Algorithms

Some axes properties control the axes grid lines. For more information on axes properties see Axes Properties.

grid sets the XGrid, YGrid, and ZGrid properties of the axes.

To control the major grid line visibility separately for each axis direction set the XGrid, YGrid, and ZGrid axes properties. These properties can be set to on or off.

```axh = gca;
set(axh,'XGrid','on')
set(axh,'ZGrid','off')```

grid minor sets the XMinorGrid, YMinorGrid, and ZMinorGrid properties of the axes.

To control the minor grid line visibility separately for each axis direction set the XMinorGrid, YMinorGrid, and ZMinorGrid axes properties. These properties can be set to on or off.

```axh = gca;
set(axh,'XMinorGrid','on')
```

To change the grid line width, set the LineWidth axes property.

```axh = gca;
set(axh,'LineWidth',2);```

By default, the number of grid lines changes when you resize a figure. To keep the same number of grid lines for any figure size, set the XTickMode, YTickMode, or ZTickMode axes properties to 'manual'.

```axh = gca;
set(axh,'XTickMode','manual');```

To customize the locations of the tick marks along an axis, set the XTick, YTick, and ZTick axes properties.

```axh = gca;
set(axh,'XTick',[0:5:50]);```

To turn on and off the display of the minor tick marks along an axis, set the XMinorTick, YMinorTick, and ZMinorTick properties to on or off.

```axh = gca;
set(axh,'XMinorTick','on')```