Display or hide axes grid lines
collapse all in page
Syntax
grid on
grid off
grid
grid minor
grid(visibility)
grid(target,___)
Description
example
grid on
displays the major grid lines for the current axes 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 majorgrid 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.
grid(visibility)
displays or hides the grid by specifying a visibility
value. You can specify visibility
as "on"
, "off"
, a logical value, or an OnOffSwitchState
value.
example
grid(target,___)
uses the axes or standalone visualization specified by target
instead of the current axes. Specify target
before all other arguments.
Examples
collapse all
Display Grid Lines
Open Live Script
Display the grid lines for a sine plot.
x = linspace(0,10);y = sin(x);plot(x,y)grid on
Remove Grid Lines
Open Live Script
Create a surface plot and remove the grid lines.
[X,Y,Z] = peaks; surf(X,Y,Z)grid off
Display Major and Minor Grid Lines
Open Live Script
Display the major and minor grid lines for a sine plot.
x = linspace(0,10);y = sin(x);plot(x,y)grid ongrid minor
Display Grid Lines on Specific Axes
Open Live Script
Starting in R2019b, you can display a tiling of plots using the tiledlayout
and nexttile
functions. Call the tiledlayout
function to create a 2-by-1 tiled chart layout. Call the nexttile
function to create the axes objects ax1
and ax2
. Plot data into each axes. Then display grid lines in the bottom plot by passing ax2
to the grid
function.
x = linspace(0,10);y1 = sin(x);y2 = sin(3*x);tiledlayout(2,1)% Top plotax1 = nexttile;plot(ax1,x,y1)% Bottom plotax2 = nexttile;plot(ax2,x,y2)grid(ax2,'on')
Input Arguments
collapse all
visibility
— Grid visibility
"on"
| "off"
| true
or 1
| false
or 0
| OnOffSwitchState
value
Grid visibility, specified one of these values:
"on"
or"off"
— A value of"on"
displays the grid, and"off"
hides it. You can also specify the character vectors'on'
or'off'
.Numeric or logical
1
(true
) or0
(false
) — A value of1
ortrue
displays the grid, and0
orfalse
hides it. (since R2024a)A matlab.lang.OnOffSwitchState value — A value of
matlab.lang.OnOffSwitchState.on
displays the grid, andmatlab.lang.OnOffSwitchState.off
hides it. (since R2024a)
target
— Target object
axes | standalone visualization | array of axes or standalone visualizations
Target object, specified as one of the following:
Any type of axes object: an
Axes
,PolarAxes
, orGeographicAxes
object.A standalone visualization that has a
GridVisible
property. For example, a heatmap chart has aGridVisible
property.An array of axes or standalone visualizations that belong to the same class. To determine the class, use the class 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 propertiesaffect the appearance of the grid lines. This table lists a subsetof axes properties related to the grid lines.
Axes Property Description XTick, YTick, ZTick Location of tick marks and major grid lines for each axis direction XGrid, YGrid, ZGrid Display of major grid lines for each axis direction XMinorGrid, YMinorGrid, ZMinorGrid Display of minor grid lines for each axis direction LineWidth Line width of grid lines, axes box outline, and tick marks GridLineStyle Major grid line style MinorGridLineStyle Minor grid line style GridColor Major grid line color MinorGridColor Minor grid line color GridAlpha Major grid line transparency MinorGridAlpha Minor grid line transparency Layer Location of grid lines in relation to the plotted data When working with polar axes, some polar axes propertiesaffect the appearance of the grid lines. This table lists a subsetof polar axes properties related to the grid lines.
PolarAxes Property Description ThetaTick, RTick Location of tick marks and major grid lines for each axis direction ThetaGrid, RGrid Display of major grid lines for each axis direction ThetaMinorGrid, RMinorGrid Display of minor grid lines for each axis direction LineWidth Width of outline, tick marks, and grid lines GridLineStyle Major grid line style MinorGridLineStyle Minor grid line style GridColor Major grid line color MinorGridColor Minor grid line color GridAlpha Major grid line transparency MinorGridAlpha Minor grid line transparency Layer Location of grid lines in relation to the plotted data
When working with geographic axes, some geographic axes properties affect the appearance of the grid lines. This table lists a subset of geographic axes properties related to the grid lines. Note that
GeographicAxes
objects do not support minor grid lines.GeographicAxes Property Description Grid Display of latitude and longitude grid lines LineWidth Line width of grid lines, box outline, and tick marks GridLineStyle Grid line style GridColor Color of grid lines GridAlpha Grid line transparency
Algorithms
The grid
function sets these graphics objectproperties to either 'on'
or 'off'
:
XGrid, YGrid, and ZGrid when working with Cartesian
Axes
objects.ThetaGrid and RGrid whenworking with
PolarAxes
objects.Grid when working with
GeographicAxes
objects.GridVisible
when working with othertypes of graphics objects, such as aHeatmapChart
object.
Version History
Introduced before R2006a
expand all
R2024a: Display or hide grid lines by specifying logical or OnOffSwitchState
value
Display or hide the grid lines by specifying the visibility
input argument as a logical value or as a matlab.lang.OnOffSwitchState value. The values 1
and true
are equivalent to "on"
, and 0
and false
are equivalent to "off"
.
The values "on"
and "off"
are still supported.
See Also
Functions
- axis | box | title | xlabel | ylabel | legend
Properties
- Axes Properties | PolarAxes Properties | GeographicAxes Properties
Classes
- matlab.lang.OnOffSwitchState
Topics
- Add Grid Lines and Edit Placement
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office