How can I change the gridline color without changing the tick and tick label colors in MATLAB?
Show older comments
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.
Accepted Answer
More Answers (3)
Image Analyst
on 13 Aug 2016
To do it in R2014b and later, try this:
y = sin([1:40]/10);
plot(y, 'm*-');
grid on;
ax = gca % Get handle to current axes.
ax.XColor = 'r'; % Red
ax.YColor = 'b'; % Blue
ax.GridAlpha = 0.9; % Make grid lines less transparent.
ax.GridColor = [0.1, 0.7, 0.2]; % Dark Green.

Jos (10584)
on 26 Feb 2014
Edited: MathWorks Support Team
on 15 May 2023
People might be interested in creating their own grid using my GRIDXY(https://www.mathworks.com/matlabcentral/fileexchange/9973-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)
Wagih Abu Rowin
on 17 Aug 2016
0 votes
ax = gca; ax.GridLineStyle = '--'; ax.GridColor = [0.1, 0.1, 0.1]; grid on
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!