Alex Rivera | Logout

Minor grid with solid lines & grey-color

Asked 2011-07-05T09:08:06.677
13

I'm using the following to display the minor grid in my plot:

grid(gca,'minor') 
set(gca,'MinorGridLineStyle','-')

but I'd like to change the color of the grid lines to a nice greyscale. I can't find any option 'grid color' in matlab... Do you know any or any workaround? I found this: http://www.mathworks.com/matlabcentral/fileexchange/9815-gridcolor but as I read of the comments, it doesn't work very well and further it only changes gridcolor, not the color of the minor grid... Thanks!


EDIT: Problem with semilogx as posting here now:

x = [1e-9 1e-8 1e-7 1e-6 1e-5 1e-4 1e-3 1e-2]';
y1 = linspace(20, 90, 8);
y2 = y1.^2;
y3 = y1./y2+5;

% plotte: http://www.mathworks.com/help/techdoc/ref/linespec.html
myfig = figure('Position', [500 500 445 356]); %[left, bottom, width, height]:
p1 = semilogx(x,y1,'x--r',x,y2,'*-b');

ax1 = gca;
set(ax1, 'Position',[0.13 0.18 0.75 0.75]);

xlim([0 max(x)]);
ylim([0 max([max(y1) max(y2)])]);


col=.85*[1 1 1];
%# create a second transparent axis, same position/extents, same ticks and labels
ax2 = axes('Position',get(ax1,'Position'), ...
    'Color','none', 'Box','on', ...
    'XTickLabel',get(ax1,'XTickLabel'), 'YTickLabel',get(ax1,'YTickLabel'), ...
    'XTick',get(ax1,'XTick'), 'YTick',get(ax1,'YTick'), ...
    'XLim',get(ax1,'XLim'), 'YLim',get(ax1,'YLim'),...
    'XScale', 'log');

%# show grid-lines of first axis, give them desired color, but hide text labels
set(ax1, 'XColor',col, 'YColor',col, ...
    'XMinorGrid','on', 'YMinorGrid','on', ...
    'MinorGridLineStyle','-', ...
    'XTickLabel',[], 'YTickLabel',[],'XScale', 'log');


%# link the two axes to share the same limits on pan/zoom
linkaxes([ax1 ax2],'xy');

Displaying like this: enter image description here</div>
                    
                    <div class= matlab colors grid plot

Edit
Report

2 Answers

3

While Amro is right that the minor grid's color is the same as that of the axis labels, you can always turn off the axis labels and overlay a second axes with transparent filling and set the labels on that in a different color. Here's a small example showing how:

plot(rand(10,1))
xTicks=get(gca,'xTick');
yTicks=get(gca,'ytick');
set(gca, 'XMinorGrid','on', 'YMinorGrid','on',...
    'XColor','r', 'YColor','g','xticklabel',[],'yticklabel',[],...
    'box','off')

h2=axes;
set(h2,'color','none','xtick',linspace(0,1,numel(xTicks)),'xticklabel',xTicks,...
    'ytick',linspace(0,1,numel(yTicks)),'yticklabel',yTicks)

enter image description here

answered 2011-07-05T12:49:56.703
2

This lets you set independent colors for major and minor X and Y grid lines, without overwriting the outer box. Even better, subsequent legend() commands will pick up the plot lines, not the manually drawn grid lines.

The trick is to make copies of the axes, then reverse their order in the figure's drawing hierarchy. Each copy of the axes can then draw its own set of grid colors and styles.

This strategy is compatible with subplot() and print().

function gridcolor(majorX, majorY, minorX, minorY)

ax1 = gca;   %# get a handle to first axis

%# create a second transparent axis, same position/extents, same ticks and labels
ax2 = copyobj(ax1,gcf);
ax3 = copyobj(ax1,gcf);

delete(get(ax2,'Children'));
delete(get(ax3,'Children'));

set(ax2, 'Color','none', 'Box','off','YTickLabel',[],'YTickLabel',[],...
    'GridLineStyle', '-',...
    'XGrid','on','YGrid','on',...
    'XMinorGrid','off','YMinorGrid','off',...
    'XColor',majorX,'YColor',majorY);
set(ax3,'Box','off','YTickLabel',[],'YTickLabel',[],...
    'MinorGridLineStyle','-',...
    'XGrid','off','YGrid','off',...
    'XMinorGrid','on','YMinorGrid','on',...
    'XColor',minorX,'YColor',minorY);

set(ax1, 'Color','none', 'Box','on')

handles = [ax3; ax2; ax1];
c = get(gcf,'Children');
for i=1:length(handles)
    c = c(find(c ~= handles(i)));
end
set(gcf,'Children',[c; flipud(handles)]);

linkaxes([ax1 ax2 ax3]);
end

subplot(211);semilogx([1:4000]);gridcolor('r','g','c','b');
subplot(212);semilogx(([1:4000]).^-1);gridcolor('r','g','c','b');

Screenshot demonstrating gridcolor()

answered 2013-06-13T18:24:48.000

Your Answer