Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Custom Grid on a Plot

1,734 views
Skip to first unread message

e r

unread,
Jun 9, 2008, 7:02:01 PM6/9/08
to
Hi,

I was wondering if anyone know how to set up a custom grid
on a plot. Basically I have this:

fig1 = plot(x, y);

axis([0 10 1 5]);

set(gca,'XGrid','on','YGrid','on','XMinorGrid','on');


So I know how to turn on the minor grid, but what I want is
lines every 0.2 unit on the x-axis and lines every 1 unit on
the Y axis.

Does anyone know how to set this up, and change the colour
of the grid line?


Thanks

Walter Roberson

unread,
Jun 9, 2008, 7:17:14 PM6/9/08
to
In article <g2kcp9$2ls$1...@fred.mathworks.com>,
e r <laxman...@hotmail.com> wrote:

>I was wondering if anyone know how to set up a custom grid
>on a plot. Basically I have this:

>fig1 = plot(x, y);

>axis([0 10 1 5]);

>set(gca,'XGrid','on','YGrid','on','XMinorGrid','on');

>So I know how to turn on the minor grid, but what I want is
>lines every 0.2 unit on the x-axis and lines every 1 unit on
>the Y axis.

>Does anyone know how to set this up,

MATLAB, at least up to R2007a, does not provide any visible way to
control the minor tick spacing.

>and change the colour
>of the grid line?

Set the axes XColor, YColor, or ZColor. Unfortunately this will change
the label colours as well.


I do not know if you could gain more control by going in at the java level.
--
amazon.com's top 8 books about "walter" are Kotzwinkle/ Gundy/ Colman's
"Walter the Farting Dog"

e r

unread,
Jun 10, 2008, 9:35:04 AM6/10/08
to
robe...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g2kdlq$m1o$1...@canopus.cc.umanitoba.ca>...
_______________________________________________________

Thanks for your reply. If you can't control the spacing of
the minor grid ticks, does anyone know if you can control
the spacing of the actual grid itself. So when I say:

axis([0 5 1 5]);

Matlab writes in

0...0.5...1...1.5...etc

as the label on the x-axis. Is there a way to change how
often the labels are shown?

Walter Roberson

unread,
Jun 10, 2008, 10:16:42 AM6/10/08
to
In article <g2lvu8$8hm$1...@fred.mathworks.com>,
e r <laxman...@hotmail.com> wrote:

>Matlab writes in

>0...0.5...1...1.5...etc

What I gather (but have not tested) is that a grid major axis
line will be drawn at the position of each XTick, YTick, ZTick
(provided the appropriate axes has grid marks enabled.)

You can set the *Tick values yourself (but if you do that,
you may want to take into account the current zoom factor.)

If you wanted grid positions that were unlabeled on the axis,
then you can set the *TickLabel property so that the label for
those positions is the empty string. This is not quite the same
as minor grid lines because minor grid lines are lighter and produce
a smaller tick mark.
--
"You may comand nature to the extent only in which you are willing to
obey her." -- Walter Russell

Jos

unread,
Jun 10, 2008, 1:34:01 PM6/10/08
to
"e r" <laxman...@hotmail.com> wrote in message <g2kcp9
$2ls$1...@fred.mathworks.com>...


My GRIDXY function provides a flexible way of creating your
grid lines:

http://www.mathworks.com/matlabcentral/fileexchange/loadFile
.do?objectId=9973&objectType=FILE

hth
Jos

hth
Jos

e r

unread,
Jun 10, 2008, 10:57:02 PM6/10/08
to
"Jos " <DEL...@jasenDEL.nl> wrote in message
<g2mdu9$b81$1...@fred.mathworks.com>...


______________________________________________________

Hey,

Thanks a lot for you replies. I already got it to work for
using the xTickLabel property, but my solution is quite
complicated. Also changing the colour creates the problem of
changing the label colour as well. I will take a look at the
gridxy() program for a better solution. Thanks.

For future reference this was my solution:

maxTime = 5; %max value of the x axis
xInv = 0.04; %interval between lines


numX = maxTime / xInv; %number of points on the x axis
xxPrint = 0:xInv:maxTime; %the value of each x-axis point

%the string that will eventually hold the X axis labels
xTickMarks = {};

%loop through the values, if it is a whole number
%add it to the string, otherwise add a blank spot
for i = 1:numX + 1
if mod(xxPrint(i), 1) == 0
xTickMarks(i) = {num2str(xxPrint(i))};
else
xTickMarks(i) = {''};
end
end

Then to plot this you would:
plot(x, y);
set(gca, 'xtick', xxPrint, 'XTickLabel', xTickMarks);
grid on;


A very ugly solution, I know. Hopefully GridXY is a lot better.

Thanks for the help.

Laxman

unread,
Jun 10, 2008, 11:20:17 PM6/10/08
to
"e r" <laxman...@hotmail.com> wrote in message
<g2netu$pt1$1...@fred.mathworks.com>...

______________________________________________________

Ok, I just tried out the gridxy() script. WOW! That was
unbelievably simple to use and extremely useful as well. I
can't believe this hasn't become standard in matlab yet.

Jos, thank you very much!! (Though I must admit, I had a
good laugh at the insanity of what I had to do to make this
work with only standard matlab functions:)

John Major

unread,
Aug 29, 2012, 9:20:08 AM8/29/12
to
"e r" <laxman...@hotmail.com> wrote in message <g2kcp9$2ls$1...@fred.mathworks.com>...
> Hi,
>
> I was wondering if anyone know how to set up a custom grid
> on a plot.
...snip...
> Thanks

I've seen a lot of people jump through hoops to get good grid lines. The solution is simple. Draw your own. The trick is to exclude them from the legend. Here is an example script

%% draw your own grid lines
X = 1:50;
Y1 = random('normal',0.5*X,1);
Y2 = Y1+random('normal',X*0,1);
figure(1); clf
for i=5:5:25 % draw horizontals
hGRID = plot([0 50],[i i],'c-');
set(get(get(hGRID,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off'); % Exclude line from legend
hold on
end
for i=5:5:45 % draw verticals
hGRID = plot([i i],[0 30],'c-');
set(get(get(hGRID,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off'); % Exclude line from legend
end
% plot main elements
plot(X,Y1,'k-+')
plot(X,Y2,'rs')
xlabel('X')
ylabel('Y')
legend('Series 1','Series 2','Location','NorthWest')
title('Draw Your Own Grids')
0 new messages