Search code examples
matlabplotmatlab-figure

How to make grid lines the same colour as tick lines


See the below example plot, the tick lines are black while grid lines are grey.

plot

How can we make them the same?

Neither of these GridColor methods helped:

%set(gca, 'GridColor', [0 0 0]);
ax = gca;
ax.GridColor = [0 0 0];

Solution

  • To change the tick marks

    See the NumericRuler properties. In short:

    ax = gca;
    ax.XAxis.Color = [.8,.8,.8]; % grey as RGB triplet
    ax.YAxis.Color = 'blue';     % blue as option keyword
    

    To change the grid lines

    You were right with ax.GridColor, but you have a semi-transparent grid by default so need to also set ax.GridAlpha.

    ax = gca;
    ax.GridColor = [0 0 0]; % Black as RGB, this is [0.15 0.15 0.15] by default (dark grey)
    ax.GridAlpha = 1;       % Opaque, this is 0.15 by default (85% transparent) 
    

    To make them the same

    ax = gca;
    % Set the grid to be opaque
    ax.GridAlpha = 1;
    % Set them to be the same colour
    myColour = [0.4, 0.7, 1]; % Any RGB triplet etc.
    ax.GridColor = myColour;
    ax.XAxis.Color = myColour;
    ax.YAxis.Color = myColour;