Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How can I change the gridline color without changing the tick and tick label colors?

Asked by MathWorks Support Team on 24 May 2013
Latest activity Commented on by Bobby on 28 Feb 2014

When I execute the following lines of code:

h= plot(rand(1, 100));
grid on
set(gca, 'XColor', 'r')

the color of the X-axis gridlines, X tick-marks and X tick-labels all become red.

I would like to selectively change the color of the gridlines alone, without changing the color of the X tick-marks and X tick-labels.

0 Comments

MathWorks Support Team

Products

2 Answers

Answer by MathWorks Support Team on 24 May 2013
Accepted answer

The gridline color cannot be changed without affecting the tick-mark and tick-label colors.

However, you can work around this issue by plotting lines of the desired color on top of your figure using PLOT. The following example demostrates this approach:

h= plot(rand(1, 100));
hold on
grid on
% Obtain the tick mark locations
xtick = get(gca,'XTick'); 
% Obtain the limits of the y axis
ylim = get(gca,'Ylim');
% Create line data
X = repmat(xtick,2,1);
Y = repmat(ylim',1,size(xtick,2));
% Plot line data
plot(X,Y,'r')

After executing the above code, the color of the X-axis gridlines remain red, whereas the X tick-marks and X tick-labels are black.

2 Comments

Russell on 26 Feb 2014

Really? This is the solution? $5k for a single seat and you can't make a basic plot formatting change like adding light grey grid lines without making all the labels light grey? I will never understand why MatLab costs so much...

Bobby on 28 Feb 2014

Agreed. So instead as another option I have to export the picture (without a grid), bring it in powerpoint and then draw gray lines? This seems like an absolute basic plot function. I mean even EXCEL can do it.

MathWorks Support Team
Answer by Jos (10584) on 26 Feb 2014

People might be interested in creating their own grid using my GRIDXY function I submitted many years ago to the File Exchange. An example:

gridxy(get(gca,'xtick'),get(gca,'ytick'),'color',[.6 .6 .6],'linewidth',1)

0 Comments

Jos (10584)

Contact us