askvity

How to Remove Grid Lines in MATLAB

Published in MATLAB Graphics 5 mins read

You can remove grid lines in MATLAB using standard commands like grid off for most plots or by finding and deleting their specific graphic handles for more granular control or for certain plot types like polar plots, as described in some documentation.

MATLAB provides flexible ways to control the appearance of plots, including the visibility of grid lines. The most common methods involve using dedicated commands or modifying the properties of the axes object.

Standard Method for Cartesian Plots

For typical 2D and 3D Cartesian plots, the easiest way to toggle grid lines is using the grid command:

  • To turn the main grid lines off:
    grid off;
  • To turn the main grid lines on:
    grid on;
  • To toggle the state (on to off, off to on):
    grid;

You can also control minor grid lines separately:

  • To turn minor grid lines off:
    grid minor off;
  • To turn minor grid lines on:
    grid minor on;
  • To toggle minor grid line state:
    grid minor;

Combining commands allows you to remove both major and minor grid lines:

% Example: Create a plot and then remove grid lines
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);

% Remove main grid lines
grid off;

% Optionally remove minor grid lines if they were on
grid minor off;

Using Axes Properties

Another way to control grid lines is by directly setting the properties of the axes object. You can get the current axes handle using gca (Get Current Axes).

The relevant properties are:

  • XGrid: Controls grid lines parallel to the y-axis (on the x-axis ticks). Set to 'on' or 'off'.
  • YGrid: Controls grid lines parallel to the x-axis (on the y-axis ticks). Set to 'on' or 'off'.
  • ZGrid: Controls grid lines for 3D plots (on the z-axis ticks). Set to 'on' or 'off'.
  • XMinorGrid, YMinorGrid, ZMinorGrid: Control minor grid lines. Set to 'on' or 'off'.

To remove grid lines using axes properties:

% Example: Create a plot and remove grid lines using axes properties
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y);

% Get the current axes handle
ax = gca;

% Turn off X and Y grid lines
ax.XGrid = 'off';
ax.YGrid = 'off';

% Optionally turn off minor grid lines
ax.XMinorGrid = 'off';
ax.YMinorGrid = 'off';

% For a 3D plot (example)
% z = x .* y;
% plot3(x, y, z);
% ax = gca;
% ax.ZGrid = 'off';
% ax.ZMinorGrid = 'off';

Removing Grid Lines in Specific Cases (e.g., Polar Plots)

While grid off works for many plot types, some documentation suggests a different approach might be necessary for specific cases like polar plots, offering more direct control over the graphic objects.

According to some documentation from August 31, 2010, for polar plots, grid lines and labels can be removed by locating their handles with the FINDALL function, and using the DELETE function to remove them. These objects reportedly exist in an axes whose Visible property has been set to 'off'.

This method involves finding specific graphic objects within the plot figure and removing them manually.

% Example: Create a polar plot
theta = linspace(0, 2*pi, 100);
rho = sin(2*theta);
polarplot(theta, rho);

% To remove grid lines and labels in a polar plot using findall/delete
% Note: This targets specific graphic objects. The types might vary slightly
% depending on the exact MATLAB version or plot contents.
h = gca; % Get handle to polar axes
% Find grid line objects within the axes (type 'hggroup' is common for polar grids)
grid_handles = findall(h, 'Type', 'hggroup', '-and', 'Tag', 'AxeGrid');
% Find radial grid lines
radial_handles = findall(h, 'Type', 'line', '-and', 'Tag', 'RadialGridLine');
% Find angular grid lines
angular_handles = findall(h, 'Type', 'line', '-and', 'Tag', 'ThetaGridLine');

% You might also need to find labels if required
% label_handles = findall(h, 'Type', 'text', '-and', 'Tag', 'AxeTickLabel');

% Delete the found objects
delete([grid_handles; radial_handles; angular_handles]);
% delete(label_handles); % Uncomment if you want to remove labels too

This method is more advanced as it requires identifying the specific types and tags of the graphic objects representing the grid lines. It provides maximum control but is less straightforward than the standard grid off command.

Summary of Methods

Method Description Applies To Ease of Use
grid off Simple command to toggle main grid visibility. Most Cartesian plots Easy
grid minor off Simple command to toggle minor grid visibility. Most Cartesian plots Easy
Axes Properties Setting XGrid, YGrid, ZGrid, etc., directly on axes object. Most Cartesian plots Moderate
findall and delete Locate specific grid line graphics objects by handle and delete them. Specific cases (e.g., polar plots as referenced), granular control Advanced

Choose the method that best suits your plot type and desired level of control. For most standard plots, grid off is the quickest way to remove grid lines.

Related Articles