Different grid lines color in MATLAB

Is it possible to change color of every single grid line? Not only for all X or Y lines.
I only know it is possible to change any label on axis like this:
ax = gca;
ax.YTickLabel{2} = '\color{green}T_{S}';

Answers (1)

The grid is a property of the current axis. After you draw your figure, and add the grid, you can set the GridColor property of the current axis. Here's a little example. Note RGB colors are normalized values in range of 0 to 1.
% make up some data to plot
x = linspace(0,100);
y = x.^2 + 3;
% plot the data and add the grid
plot(x,y)
grid
% change the grid color, gca means get current axis, the colors are RGB values
set(gca,'GridColor',[0.1 0.2 0.9]) % a bluish color

9 Comments

I think the OP was asking how to change the color of each individual grid line.
Yes, I am interested in changing a color of a certain grid line, not all of them
You could draw ordinary lines to overlay the existing grid lines for your special grid lines, and then set their colors as you like. By the way, I'm assuming you mean grid lines and not tick marks, your example in your questions seems to refer to tick marks.
Here is an example:
% plot the data and add the grid
plot(x,y)
grid
% get the handle to the current axes
ax = gca;
% find out the range of y values, so the line you draw matches the current
% plot
yRange = ylim
% draw special grid lines
% parameters for special grid lines
color = {[0.9, 0.3, 0.8];[0, 0 1]}; % RGB colors for the special grid lines
xGrid = [20,60]; % x locations of special grid line
% generate line that overlay the current grid
numGrid = length(xGrid);
xGridLine = repmat(xGrid,numGrid,1);
yGridLine = repmat(yRange(:),1,numGrid);
% add the special grid lines to the plot and set their colors
hold on
h = plot(xGridLine,yGridLine);
set(h,{'Color'},color)
hold off
Please see the accepted answer to https://www.mathworks.com/matlabcentral/answers/19815-explicitly-specifying-line-colors-when-plotting-a-matrix which is where I learned how to set the colors when you plot a matrix of x and a matrix of y values
If you draw individual lines, you'll need to set the xlim and ylim first as well as the xtick and ytick. Note that this differs from the grid in that the grid is adaptive to changes in your plot whereas your drawn line will not be adaptive.
You don't need to set the xlim and ylim, you can leave that to autoscale, but yes you must use whatever the current ylim range is for the overlayed line. That is why in my example I assigned the y range of the overlayed grid line to match the current yLim
Why not just use the xline and yline functions to create these auxiliary multicolored grid lines?
Excellent suggestion! That would definitely be cleaner. I actually never realized that xline and yline functions were available. I'm glad to have learned something new too, thanks.
Adam Danz
Adam Danz on 1 Jul 2019
Edited: Adam Danz on 1 Jul 2019
xline & yline are great solutions for r2018b or later and those lines will extend if the axis limits are changed after they are drawn.
If you're plotting lines manually (as Jonathan proposed), the lines will not extend if the axis limits are increased which is why it's good to either set the desired axis limits prior to drawing those lines or make sure the line extend to the desired limits.
Okay, so it seems I have to create a custom grid of lines using 'yline' after all.

Sign in to comment.

Products

Release

R2018b

Asked:

on 1 Jul 2019

Commented:

on 1 Jul 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!