Thread Subject: Change plot grid line colour

Subject: Change plot grid line colour

From: Stuart

Date: 20 Nov, 2009 10:53:04

Message: 1 of 4

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

Subject: Change plot grid line colour

From: Wayne King

Date: 20 Nov, 2009 11:23:08

Message: 2 of 4

"Stuart " <wybrow@aol.com> wrote in message <he5seg$9fr$1@fred.mathworks.com>...
> 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

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

Subject: Change plot grid line colour

From: gwideman

Date: 11 Apr, 2011 09:54:04

Message: 3 of 4

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.

Subject: Change plot grid line colour

From: gwideman

Date: 11 Apr, 2011 10:47:04

Message: 4 of 4

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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
gridlines gwideman 11 Apr, 2011 06:49:37
grid lines gwideman 11 Apr, 2011 06:49:37
rssFeed for this Thread

Contact us at files@mathworks.com