File Exchange

image thumbnail


version 1.3 (31.9 KB) by

Change color of grid lines without changing color of axis labels or box

9 Ratings



View License

Use this function to change the color of gridlines to a color different to the label and box color, which is currently not implemented in Matlab.

The general syntax for a call is:

gridcolor([ax1, ax2, ...], c_x, c_y, c_z)

Here ax1, ... are handles to existing axes (optional argument) and c_xyz are color specifications (e.g. 'r' or [1 0 0], also optional). If no axis handle is supplied, the call goes to the current axis (gca), if no color is supplied a standard color, which is defined in the first executable line of the file, is used.
The function should be called after the plot has been created and the grid has been turned on.

grid on
gridcolor([1 0 0], 'b')

This function is no longer tested for Matlab 6.5 or lower and will most likely not work for these versions. It should run on Matlab 7.0 or higher.

For further examples please refer to the examples in the header of the file.


PS.: I have included some lines from Yair Altmans "PropListener" example

Comments and Ratings (17)


Becca (view profile)

Overall it does what I need (had to fix the label bug but that was easy). However, it re-sizes my axes so that they are square. I have to have my plots a specific size/shape (figure('position',[10 10 8.5 8.5*0.8]) in cm). This causes my xlabel to get cut off. Does anybody have a fix for it?

jon smith

i found two errors. it changes the axes titles as well and add it closer to the axis. it also messes up subplots, the axes get completely reconfigured

This script resizes my plot and adds the labels at wrong position in grid color. Therefore it is not applicable for me.

Sebastian Hölz

hey Carey, this works fine for me ... :
>> load clown
>> imagesc(X)
>> gridcolor('w')
>> grid on
You need to be more specific with your problem. Besides ... before giving a crap rating I would spend a minute and maybe ask the author directly for help ...
Just a thought

Carey Smith

This did not work will for me with an imagesc plot. (ver. 7.10)
It created ghost versions of my X & Y labels.
Use gridxy.m instead (with the updates I am submitting today).


Ondrej (view profile)

I have problems with zooming. When I use gridcolor I can't zoom the plots in figure, instead the whole box is getting smaller and smaller.


Ondrej (view profile)

Junwei Du


For my version(7.8.0),it changes the figure axes size.

abhi mani

SYNTAX: <gridaxeshandle>=cgrid(<colorarg, stylearg, colorargy>)

abhi mani

SYNTAX: <gridaxeshandle>=cgrid(<colorarg, stylearg, colorargy>)

Sebastian Hölz

In reply to Joanne Hung and Nitika:
It seems that my update did not make it to the FEX. I have just posted a new version (0.99), which removes the mentioned bugs.

Furthermore, this version also removes all previous restrictions and should now fit nicely into the general Matlab framework. I would recommend everyone with old versions to update, once the new version has been uploaded.



Nitika (view profile)

Sebastian, I just downloaded your function file and still have the problem that Joanne is mentioning. I have MatlabR2008a.

Sebastian Hölz

In reply to Joanne Hung:
I had fixed this bug a while ago, but forgot to update it here on the FEX. It was caused by a call to Matlabs "version"-command, which has somewhat changed its bevavior ... file is updated and should work as expected!

Alan B

Can't you just get the current tick values and plot lines of the desired color with endpoints determined by the current limits?

Joanne Hung

For my version (v7.5), it does not support "linkaxes". The box changes the color after using the function. If using xlabel or ylabel before grid, there are two labels on one axis and the second label has the color applied on the grid

urs (us) schwarz

with the added help it's very nice and useful
the nasty behavior in certain (anomalous) situations cannot be taken care of easily - but the author provides a valid (and hopefully temporary) workaround

urs (us) schwarz

nice idea
there are some issues that need to be addressed:
- the help section does NOT work at all (wrong indentation of the %-markers)
- once GRIDCOLOR was run for an axis, a subsequent call to PLOT will spit out numerous annoying warnings...



Version 0.99
- No support for Matlab 6.5 or lower anymore
- Should now work as general purpose functions without restrictions
- Removed some bugs


09.01.2009 Fixed bug found by Christian S. Hansen. This prevented the axis and labels to be displayed correctly.

Update to fix bug in recent Matlab versions.

- changed stacking order of "reference" (now bottom) and "gridcolor" (now top) axis
- removed linkaxes command for Matlab 6.5, since it is not included in this release

- Fixed help bug. Error mentioned by Urs. Fixes: hold on - after first plot command or Use low level commands ('line' instead of plot). Use gridcolor as last command when creating a plot.

MATLAB Release
MATLAB 7.0.4 (R14SP2)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video