带有实线的小网格&灰色色

时间:2011-07-05 09:08:07

标签: matlab colors grid plot

我正在使用以下内容在我的情节中显示次要网格:

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

但我想将网格线的颜色更改为漂亮的灰度。我在matlab中找不到任何“网格颜色”选项...你知道任何或任何解决方法吗? 我发现了这个:http://www.mathworks.com/matlabcentral/fileexchange/9815-gridcolor但是当我读到评论时,它不能很好地工作,而且它只会改变网格颜色,而不是小网格的颜色...... 谢谢!


修改: 现在semilogx在此处发布的问题:

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');

像这样显示: enter image description here


EDIT2:添加第二个y轴时会出现问题,如下图所示,请查看右侧y轴的刻度:

enter image description here

这将在这里讨论以获得更好的概述! Matlab: Problem with ticks when setting minor grid style and two y-axis

4 个答案:

答案 0 :(得分:10)

设置'XColor','YColor'轴属性。请注意,这些属性决定了轴线,刻度线,刻度线标签和轴网格线的颜色,因此AFAIK不能指定那些不同于整个轴的颜色。

实施例

plot(rand(10,1))
set(gca, 'XMinorGrid','on', 'YMinorGrid','on', 'XColor','r', 'YColor','g')

EDIT1:

您始终可以创建第二个透明轴,其中包含所需的网格颜色,但没有刻度或标签,堆叠在当前轴的顶部。这是一个例子:

%# create plot as usual
plot(rand(10,1))
hAx1 = gca;

%# create a second axis, same position/extents, no tick or labels, colored grid-lines
hAx2 = axes('Position',get(hAx1,'Position'), ...
    'Color','none', 'TickLength',[1e-100 1e-100], ...
    'XMinorGrid','on', 'YMinorGrid','on', ...
    'Box','off', 'XColor','g', 'YColor','r', ...
    'XTickLabel',[], 'YTickLabel',[], ...
    'XTick',get(hAx1,'XTick'), 'YTick',get(hAx1,'YTick'), ...
    'XLim',get(hAx1,'XLim'), 'YLim',get(hAx1,'YLim'));

%# position it on top
%#uistack(hAx2,'top')

%# redraw the enclosing box in the original axis colors
x = get(hAx1,'XLim');
y = get(hAx1,'YLim');
line([x([1 2]) nan x([2 1])],[y([1 1]) nan y([2 2])],'Color',get(hAx1,'XColor'))
line([x([1 1]) nan x([2 2])],[y([1 2]) nan y([2 1])],'Color',get(hAx1,'YColor'))

唯一的问题是网格线是在绘图的顶部绘制的,如果网格线很粗,可能会妨碍网格线:)

edit1_screenshot


EDIT2:

似乎@yoda与上面有类似的想法。这是一个略微改进的版本,灵感来自他的解决方案:

%# create plot as usual
plot(11:20, rand(10,1)*5)
hAx1 = gca;   %# get a handle to first axis

%# create a second transparent axis, same position/extents, same ticks and labels
hAx2 = axes('Position',get(hAx1,'Position'), ...
    'Color','none', 'Box','on', ...
    'XTickLabel',get(hAx1,'XTickLabel'), 'YTickLabel',get(hAx1,'YTickLabel'), ...
    'XTick',get(hAx1,'XTick'), 'YTick',get(hAx1,'YTick'), ...
    'XLim',get(hAx1,'XLim'), 'YLim',get(hAx1,'YLim'));

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

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

%# lets create a legend, and some titles
legend(hAx1, 'text')
title('title'), xlabel('x'), ylabel('y')

edit2_screenshot


EDIT3(取2):

这是相同的示例,但具有对数刻度x轴。请注意,不是创建第二个轴而是手动设置其属性以匹配第一个轴,而只是copyobj轴,并删除它的子项。

%# create a plot as usual (x-axis is in the log-scale)
semilogx(logspace(0,5,100), cumsum(rand(100,1)-0.5))
xlabel('x'), ylabel('y'), title('text')
legend('plot')

%# capture handle to current figure and axis
hFig = gcf;
hAx1 = gca;

%# create a second transparent axis, as a copy of the first
hAx2 = copyobj(hAx1,hFig);
delete( get(hAx2,'Children') )
set(hAx2, 'Color','none', 'Box','on', ...
    'XGrid','off', 'YGrid','off')

%# show grid-lines of first axis, style them as desired,
%# but hide its tick marks and axis labels
set(hAx1, 'XColor',[0.9 0.9 0.9], 'YColor',[0.9 0.9 0.9], ...
    'XMinorGrid','on', 'YMinorGrid','on', 'MinorGridLineStyle','-', ...
    'XTickLabel',[], 'YTickLabel',[]);
xlabel(hAx1, ''), ylabel(hAx1, ''), title(hAx1, '')

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

%# Note that `gca==hAx1` from this point on...
%# If you want to change the axis labels, explicitly use hAx2 as parameter.

您应该使用此代码在示例中获得正确的绘图。但是我认为您选择的x变量值可能在当前图形尺寸上太近以显示所有垂直线(只需最大化图形以查看我的意思)...

edit3_screenshot

为了更好地了解每个轴包含的内容,这里是一个分割视图,其中左侧的图表仅包含hAx1呈现的图形,而右侧的图表仅包含hAx2组件。在前面显示的最终图中,这两个视图基本上叠加在一起。

edit3_screenshot_divided_axes

答案 1 :(得分:6)

不幸的是,虽然过度或不足第二个网格轴的技巧大部分都有效,但是当您保存为PDF文件时,Matlab无法正确渲染它。这是因为Matlab不支持PDF中的透明度。

一种解决方法是简单地使用line逐个绘制网格线:

for dir='XY';
    ticks = get(gca, [dir 'Tick']);
    lim = get(gca, [dir 'lim']);
    for ii=1:length(ticks)
        coord = ticks(ii);

        for jj=1:9,
            if jj==1                  % major grid properties
                color = [1 1 1]*0.9;
                weight = 2;
            else                      % minor grid properties
                color = [1 1 1]*0.9;
                weight = 1;
            end
            if jj*coord > lim(2)
                continue
            end
            if dir=='X' 
                L = line((jj*coord)*[1 1], get(gca, 'ylim'), ...
                         'color', color, 'linewidth', weight);
            else
                L = line(get(gca, 'xlim'), (jj*coord)*[1 1], ...
                         'color', color, 'linewidth', weight);
            end
            uistack(L, 'bottom');
        end
    end
end

这种方法的一个缺点是它会覆盖刻度线和绘图边界框。解决这个问题的方法是将这种方法与第二轴的底层技巧结合起来。在底层轴上绘制假网格。这在Excel中正确呈现:

enter image description here

答案 2 :(得分:3)

虽然Amro是正确的,小网格的颜色与轴标签的颜色相同,但您始终可以关闭轴标签并使用透明填充覆盖第二个轴,并将标签设置为不同的颜色。这是一个小例子,展示了如何:

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

答案 3 :(得分:2)

这使您可以为主要和次要X和Y网格线设置独立颜色,而不会覆盖外部框。更好的是,后续的legend()命令将获取绘图线,而不是手动绘制的网格线。

诀窍是制作轴的副本,然后在图形的图纸层次中颠倒它们的顺序。然后,每个轴的副本都可以绘制自己的一组网格颜色和样式。

此策略与subplot()和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()