带有实线的小网格和灰色 [英] Minor grid with solid lines & grey-color

查看:14
本文介绍了带有实线的小网格和灰色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

grid(gca,'minor')设置(gca,'MinorGridLineStyle','-')

但我想将网格线的颜色更改为漂亮的灰度.我在matlab中找不到任何选项网格颜色"......你知道任何或任何解决方法吗?我发现了这个:

解决方案

设置 有与上述类似的想法.以下是受他的解决方案启发而略有改进的版本:

%# 照常创建绘图情节(11:20,兰德(10,1)* 5)hAx1 = gca;%# 获取第一个轴的句柄%# 创建第二个透明轴,相同的位置/范围,相同的刻度和标签hAx2 = 轴('位置',get(hAx1,'位置'), ...'颜色','无','盒子','开',...'XTickLabel',get(hAx1,'XTickLabel'), 'YTickLabel',get(hAx1,'YTickLabel'), ...'XTick',get(hAx1,'XTick'), 'YTick',get(hAx1,'YTick'), ...'XLim',get(hAx1,'XLim'),'YLim',get(hAx1,'YLim'));%# 显示第一个轴的网格线,给他们想要的颜色,但隐藏文本标签设置(hAx1,'XColor','g','YColor','r',...'XMinorGrid','on', 'YMinorGrid','on', ...'XTickLabel',[], 'YTickLabel',[]);%# 链接两个轴以共享相同的平移/缩放限制链接轴([hAx1 hAx2],'xy');%# 让我们创建一个图例和一些标题图例(hAx1,'文本')标题('标题'),xlabel('x'),ylabel('y')

<小时>

EDIT3(取 2):

这是相同的示例,但带有对数刻度的 x 轴.请注意,我不是创建第二个轴并手动设置其属性以匹配第一个轴,而是简单地 copyobj 轴,并删除其子级.

%# 像往常一样创建一个绘图(x 轴在对数刻度中)semilogx(logspace(0,5,100), cumsum(rand(100,1)-0.5))xlabel('x'), ylabel('y'), 标题('text')图例('情节')%# 捕获当前图形和轴的句柄hFig = gcf;hAx1 = gca;%# 创建第二个透明轴,作为第一个的副本hAx2 = copyobj(hAx1,hFig);删除(获取(hAx2,'儿童'))设置(hAx2,颜色",无",框",开",...'XGrid','off', 'YGrid','off')%# 显示第一个轴的网格线,根据需要设置样式,%# 但隐藏它的刻度线和轴标签设置(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, ''), 标题(hAx1, '')%# 链接两个轴以共享相同的平移/缩放限制链接轴([hAx1 hAx2],'xy');%# 注意 `gca==hAx1` 从现在开始...%# 如果要更改轴标签,请显式使用 hAx2 作为参数.

您应该使用此代码在示例中获得正确的绘图.但是我认为您选择的 x 变量值在当前图形大小中可能太接近而无法显示所有垂直线(只需最大化图形即可了解我的意思)...

为了更好地了解每个轴包含的内容,这是一个分割视图,其中左侧的图仅包含由 hAx1 渲染的图形,而右侧的图仅包含 hAx2 组件.在之前显示的最终图中,这两个视图基本上是相互重叠的.

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:


EDIT2: A problem occurs when adding a second y-axes as in the following picture, look at the ticks of the right y-axes:

this will be discussed here to have a better overview! Matlab: Problem with ticks when setting minor grid style and two y-axis

解决方案

Set the 'XColor','YColor' axes properties. Note that these properties determine the color of the axis lines, tick marks, tick mark labels, and the axis grid lines, so AFAIK you can't assign those different colors than that of the entire axis..

Example:

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


EDIT1:

You can always create a second transparent axis with the desired grid colors, but with no ticks or labels, stacked on top of the current axis. Here is an example:

%# 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'))

The only problem is that the grid lines are drawn on top of your plot, which might get in the way if the grid-lines are thick :)


EDIT2:

Seems like @yoda had a similar idea to the above. Here is a slightly improved version inspired by his solution:

%# 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')


EDIT3 (take 2):

Here is the same example but with a log-scale x-axis. Note how instead of creating a second axis and manually setting its properties to match the first, I simply copyobj the axis, and delete its children.

%# 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.

You should get the correct plot in your example with this code. However I think the x variable values you choose might be too close in the current figure size to show all the vertical lines (simply maximize the figure to see what I mean)...

To get a better idea of what each axis contains, here is a divided view where the plot on the left contains only the graphics rendered by hAx1, while the plot on right contains only the hAx2 components. Those two views are basically overlayed on top of each other in the final figure shown before.

这篇关于带有实线的小网格和灰色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆