Grid lines for 2-D and 3-D plots
Display the grid lines for a sine plot.
x = linspace(0,10); y = sin(x); figure plot(x,y) grid on
Create a surface plot of the peaks function.
Remove the grid lines from the surface plot.
Define a vector of random numbers.
rng default randnumbers = rand(1,20);
Plot the vector. Display the major and minor grid lines on the graph.
figure plot(randnumbers) grid on grid minor
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'.
ax = gca; ax.XGrid = 'on'; ax.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'.
ax = gca; ax.XMinorGrid = 'on';
To change the grid line width, set the LineWidth axes property.
ax = gca; ax.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'.
ax = gca; ax.XTickMode = 'manual';
To customize the locations of the tick marks along an axis, set the XTick, YTick, and ZTick axes properties.
ax = gca; ax.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'.
ax = gca; ax.XMinorTick = 'on';