Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

grid

Display or hide axes grid lines

Syntax

grid on
grid off
grid
grid minor
grid(target,___)

Description

example

grid on displays the major grid lines for the current axes or chart returned by the gca command. Major grid lines extend from each tick mark.

example

grid off removes all grid lines from the current axes or chart.

grid toggles the visibility of the major grid lines.

example

grid minor toggles the visibility of the minor grid lines. Minor grid lines lie between the tick marks. Not all types of charts support minor grid lines.

example

grid(target,___) uses the axes or chart specified by target instead of the current axes or chart. Specify target as the first input argument. Use single quotes around other input arguments, for example, grid(target,'on').

Examples

collapse all

Display the grid lines for a sine plot.

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

Create a surface plot and remove the grid lines.

[X,Y,Z] = peaks; 
surf(X,Y,Z)
grid off

Display the major and minor grid lines for a sine plot.

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

Create a figure with two subplots and assign the Axes objects to the variables ax1 and ax2. Plot a sine wave in each subplot. Then display the grid lines on the upper subplot by specifying ax1 as an input argument to the grid function.

x = linspace(0,10);
y = sin(x);
ax1 = subplot(2,1,1);
plot(ax1,x,y)

y2 = sin(3*x);
ax2 = subplot(2,1,2);
plot(ax2,x,y2)

grid(ax1,'on')

Input Arguments

collapse all

Target axes or chart, specified as an Axes object, a PolarAxes object, or a graphics object that has a GridVisible property. For example, you can add or remove grid lines from a HeatmapChart object using the grid function.

If you do not specify the target, then the grid function affects the graphics object returned by the gca command.

Tips

  • When working with Cartesian axes, some axes properties affect the appearance of the grid lines. This table lists a subset of axes properties related to the grid lines.

    Axes PropertyDescription
    XTick, YTick, ZTickLocation of tick marks and major grid lines for each axis direction
    XGrid, YGrid, ZGridDisplay of major grid lines for each axis direction
    XMinorGrid, YMinorGrid, ZMinorGridDisplay of minor grid lines for each axis direction
    LineWidthLine width of grid lines, axes box outline, and tick marks
    GridLineStyleMajor grid line style
    MinorGridLineStyleMinor grid line style
    GridColorMajor grid line color
    MinorGridColorMinor grid line color
    GridAlphaMajor grid line transparency
    MinorGridAlphaMinor grid line transparency
    LayerLocation of grid lines in relation to the plotted data

  • When working with polar axes, some polar axes properties affect the appearance of the grid lines. This table lists a subset of polar axes properties related to the grid lines.

    PolarAxes PropertyDescription
    ThetaTick, RTickLocation of tick marks and major grid lines for each axis direction
    ThetaGrid, RGridDisplay of major grid lines for each axis direction
    ThetaMinorGrid, RMinorGridDisplay of minor grid lines for each axis direction
    LineWidthWidth of outline, tick marks, and grid lines
    GridLineStyleMajor grid line style
    MinorGridLineStyleMinor grid line style
    GridColorMajor grid line color
    MinorGridColorMinor grid line color
    GridAlphaMajor grid line transparency
    MinorGridAlphaMinor grid line transparency
    LayerLocation of grid lines in relation to the plotted data

Algorithms

The grid function sets these graphics object properties to either 'on' or 'off':

  • XGrid, YGrid, and ZGrid when working with Cartesian Axes objects.

  • ThetaGrid and RGrid when working with PolarAxes objects.

  • GridVisible when working with other types of graphics objects, such as a HeatmapChart object.

Introduced before R2006a

Was this topic helpful?