File Exchange

## GRIDXY

version 2.4.0.0 (3.71 KB) by
Plot horizontal / vertical grid reference lines

Updated 11 Dec 2017

GRIDXY(X) plots vertical grid lines at the positions specified by X. GRIDXY(X,Y) also plots horizontal grid lines at the positions specified by Y. GRIDXY(..., 'Prop1','Val1','Prop2','Val2', ...) uses the properties and values specified for color, linestyle, etc.
Examples:
plot(10*rand(100,1), 10*rand(100,1),'bo') ; % random points
gridxy([1.1 3.2 4.5],'Color','r','Linestyle',':') ; % horizontal red dashed grid
h = gridxy([],[2.1:0.7:5 8],'Color',[0.9 1.0 0.2],'linewidth',3) ; % vertical solid thicker yellowish grid, and store the handle

GRIDXY can be used to plot a irregular grid on the axes.

### Cite As

Jos (10584) (2021). GRIDXY (https://www.mathworks.com/matlabcentral/fileexchange/9973-gridxy), MATLAB Central File Exchange. Retrieved .

Gwendal

Jos (10584)

@Rodolfo: ax = gca ; gridxy(ax.XAxis.MinorTickValues,[]) ...

Rodolfo

How do I use this function to allow plotting minor grids as well? I used GRIDXY in a simple loglog plot, but it only displayed the major lines. For example:

x = logspace(-1,2);
y = exp(x);
loglog(x,y,'-s')
gridxy(get(gca,'xtick'),get(gca,'ytick'),'color',[0.5 0.5 0.5],'linewidth',1,'linestyle',':')

emilio_c_b

Nice! A good workaround to overcome a little limitation of Matlab in plots edition.
Recommended command:
gridxy(get(gca,'xtick'),get(gca,'ytick'),'color',[.4 .4 .4],'linewidth',1,'linestyle',':')

Joey Low

It's just what I'm looking for! Many thanks!

Elena Gagarina

Alexander Brune

Nice.
Works on R2014a as well.

Bluewater

works for me (R2011b)
Good suggestion from Carey Smith:
gridxy(get(gca,'XTick'),get(gca,'YTick'), 'Color',[1,1,1]*0.4,'Linestyle',':');

Roger Parkyn

Is anyone else having trouble running this on Release 2014B? I have just upgraded to that version and I now find that the uistack function (i.e. where this function puts the new grid-lines at the bottom) takes a very long time to run (I did have a large number of objects but this problem didn't happen on my previous Matlab version).

Qinghua

Thank you very much for the sharing. This is also the one that I am looking for.

/Qinghua

Christian

Dexters

Exactly the one I was looking for
Thanks!!!

Jack

Let me just chime in with all the others...

Great routine! This should be included in Matlab.

Kevin J. Delaney

Fantastic--just what I needed!

Christine J. Lee

Roger Parkyn

Worked for me thanks.

Carey Smith

% I had to comment-out uistack(h,'bottom') for an imagesc plot
% (Otherwise, the grid lines are not visible.)
% These examples worked for me & simplified some aspects of using this function:
gridxy(get(gca,'XTick'),get(gca,'YTick'), 'Color',[1,1,1]*0.4,'Linestyle',':');

gridxy(get(gca,'XTick'),get(gca,'YTick'), 'Color','y','Linestyle',':');

Jos (10584)

@Chiara. You can use, e.g.,
h(1) = gridxy(MedianValue ,'color','r')
h(2) = gridxy(MeanValue ,'color','b')
legend(h,{'Median','Mean'})

hth
Jos

Chiara

Very useful - but i'd need to add a legend with entries for the vertical bars. EG i have a histogram, and two vertical bars, say one for the mean and one for the median. How can i do this?

Jos (10584)

@Phil, in 3D the lines would become planes. Is this what you want? You can also take a look at REFLINEXYZ to see if that better fills your needs:
http://www.mathworks.com/matlabcentral/fileexchange/16746

Phil

Hello everyone,

this code is exactly what I need. Unfortunately I need it in three dimensions. Does any body have an idea?

Otherwise I will try to extend this m-file but I have a feeling that this will take me lots of time

thanks

Philipp

Hoi Wong

Great Program. It'd be even better if it can accept an axes handle as its first input. For the moment, since I need it in a hurry, I tapped into your code and changed this:

function hh = gridxy(hca, x,varargin)

if(isempty(hca))
hca=get(get(0,'currentfigure'),'currentaxes')
end

Laxman P

Very nicely done. This is what should have been in matlab. Thanks for you efforts!!

Mo Al Lawati

Perfect, exactly what I needed !

Rhymer Yang

This is what I need!! Works very well!! Thanks a lot!!

Fred De Masi

Just what I needed, cheers

Jos (the author)

The 'undesired behavior' mentioned by Jim Gerrling has been fixed in v2.2

Jim Gerrling

You can fix the axis issue by
set(gca,'Layer','top')

Jim Gerrling

Unfortunaltely, the script plots over the Ticks and the axis (let's say you use green gridlines, they print over the ticks and axis).

Abderrezak Bouchedda

very good, thanks

David Ward

Just what I needed, again a very useful script, thanks

Alan Eskovitz

Thank you for a very useful utility. It works well in all of my applications so far.

Peter Nave

Very useful!

##### MATLAB Release Compatibility
Created with R2017b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux