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

Change plot grid line colour

433 views
Skip to first unread message

Stuart

unread,
Nov 20, 2009, 5:53:04 AM11/20/09
to
Hi,

I've got a the following to make a plot:

figure
plot(time,pres,'k');
xlabel('Time (s)');
ylabel('Pressure (Pa)');
grid on;

But how can I change the grid line colour from black to grey so that are less obtrusive?

Thanks

Wayne King

unread,
Nov 20, 2009, 6:23:08 AM11/20/09
to
"Stuart " <wyb...@aol.com> wrote in message <he5seg$9fr$1...@fred.mathworks.com>...

Hi Stuart, you can use set(gca,'Xcolor',..) and set(gca,'Ycolor',...)

time = 1:100; pres = randn(1,100);


plot(time,pres,'k');
xlabel('Time (s)');
ylabel('Pressure (Pa)');
grid on;

set(gca,'Xcolor',[0.5 0.5 0.5]);
set(gca,'Ycolor',[0.5 0.5 0.5]);

% Note the the X and Y ticks will be the same color as the grid lines, if you still want the tick marks black, you can do the following

Caxes = copyobj(gca,gcf);
set(Caxes, 'color', 'none', 'xcolor', 'k', 'xgrid', 'off', 'ycolor','k', 'ygrid','off');

Hope that helps,
Wayne

gwideman

unread,
Apr 11, 2011, 5:54:04 AM4/11/11
to
Wayne's method creates a copy of the axes (and the plot) in order to be able to assign separate colors to the second axes and its value labels.

But this only partly works -- having created the second axes (and plot) these may or may not stay scaled exactly the same as the first axes, as the graph is resized.

Basically not a useful method when I tried it, but might be "good enough" in somce cases, especially of you go to more effort to disable all the parts of the second axes that you don't need.

All sympathies to the previous posters though -- having the gridline color is tied to the label color seems like a pretty dumb design.

gwideman

unread,
Apr 11, 2011, 6:47:04 AM4/11/11
to
In case someone else stumbles in here with the same need to control the girdlines color independent of the tick label color -- here's a possibility.

Assuming that you are creating plots from your own code, this solution adds relatively little extra rubbish to your program, it's conceptually easy, and robust-ish.

The big solution is.... just turn off the official grid lines, and plot your own using one or another plot function, depending on the 2D or 3Dness of your plot!

If you set your axis limits and tick marks explicitly, then of course you already know where you need to plot the lines.

If you didn't, then your code has to do a bit more work to read XLim, YLim, and XTick and YTick from the axes.

-- Graham

Adekanle Oluwaseun

unread,
May 18, 2017, 12:38:09 PM5/18/17
to
"Stuart" wrote in message <he5seg$9fr$1...@fred.mathworks.com>...
set(gca,'GridColor',[0 0 0])

Paul Mennen

unread,
May 18, 2017, 11:12:08 PM5/18/17
to
gwideman wrote:

> All sympathies to the previous posters though -- having the
> gridline color is tied to the label color seems like a pretty dumb design.

Yes that design decision was lamented for perhaps a couple of decades, although it was finally fixed as of version 2014b.

"Adekanle Oluwaseun" wrote
> set(gca,'GridColor',[r g b])

Yes, that will work of course, but many people still use version 2014a or older and so that can't take advantage of that property. Another solution that works with the older and newer versions alike is to use an alternate plotting interface called "plt" on the file exchange. The default grid lines are not intrusive so you probably won't want to change them, but you can if you want to with one additional parameter (spelled out in the help file). This alone may not be enough of an advantage to justify trying out a new plotting interface, but there are many dozens of improvements over the standard matlab plotting tools and nobody has ever regretted trying it out (to my knowledge). Run the demo (demoplt.m) to get a quick sense of whether it will be worth your time to investigate further.

~Paul
0 new messages