Matlab 不提供对次要网格和刻度的太多控制。您可以打开或关闭它们的可见性(显然您已经受到了限制),但您不能设置它们的值/位置/数量等...
解决方法是完全关闭包含pcolor 绘图的轴上的所有刻度和网格,然后在其顶部创建一个透明的空axes,并使用您定义的网格属性。
因为在这个空的axes 中没有绘图或其他图形对象,Matlab 会在您设置的属性方面表现得更好(它不会尝试变得聪明并在后台更改一些东西)。
所以对于你的情况,它会是这样的:
[x,y] = meshgrid(logspace(-1,1,10),logspace(0,7,10));
pcolor(x,y,x.*y); shading interp;
colormap(flipud(gray(64))) %// just so the grid lines are more visible
axbot = gca ; %// retrieve the handle of the current axis
set(axbot, 'XScale', 'log', 'YScale', 'log');
axis off %// remove all ticks/grid etc...
%// now create the "overlay" axes, which replicate some of the properties of the underlying axis (position/limits etc ...)
axtop = axes('Position',get(axbot,'Position'),'Color','none',...
'Xlim',get(axbot,'XLim'), 'Ylim',get(axbot,'YLim'),...
'XScale', 'log', 'YScale', 'log' , ...
'YMinorTick','on' , 'YMinorGrid','off') ;
请注意,有些属性只是从底层axes 复制而来,而其他一些属性则是显式设置的(如YMinorTick 和YMinorGrid)。
这将绘制:
如果您还希望显示 Y 小网格,只需将 YMinorGrid 更改为 on。
如果您还想要右侧的 Y 刻度标记,您只需(几乎)重复相同的操作。添加另一个轴,这次指定 XAxisLocation 到 Top 和 YAxisLocation 到 right。
axtopright = axes('Position',get(axbot,'Position'),'Color','none',...
'Xlim',get(axbot,'XLim'), 'Ylim',get(axbot,'YLim'),...
'XScale', 'log', 'YScale', 'log' , ...
'YMinorTick','on' , 'YMinorGrid','off' , ...
'XAxisLocation', 'Top','YAxisLocation','right' ) ;