此问题仅适用于unix matlabs,Windows用户将无法重现它.
我在尝试创建位于y轴标签顶部的数据提示时遇到了麻烦.下图说明了这个问题:
 
 
如您所见,靠近ylabel创建的数据提示将深入到ylabel文本,而欲望效果则相反:数据提示位于轴标签的顶部.
我使用以下(不是那么简单的)代码生成了该图,下面是可用的.您可以删除评论的行% may be removed,或者甚至只是将数据提示放在-78而不是循环上以实现更快的测试脚本,但是如果有人有一天希望它创建自定义数据提示,我会保留此代码(在这种情况下,考虑一下http://undocumentedmatlab.com/blog/controlling-plot-data-tips/):   
gradientStep = 1e-1;
x=-100:gradientStep:100; xSize=numel(x);
y=x.^3-x.^2;
figH=figure(42);
lineH=plot(x,y);
ylabel('YLabel (YUnits)','FontSize',16)
xlabel('XLabel (XUnits)','FontSize',16)
dcH=datacursormode(figH);
nTips = 20; % May change the loop for a datatip at x=-78.
for pos = round(linspace(2,xSize,nTips))
  datatipH=dcH.createDatatip(lineH,...
    struct('Position',[x(pos) y(pos)]));
  orientation = 'top-left';
  if pos>1
    tipText{1} = 'The grandient here is: ';
    tipText{2} = ['\Deltax:',sprintf('%d',x(pos)-x(pos-1)),' XUnits'];
    tipText{3} = ['\Deltay:',sprintf('%d',y(pos)-y(pos-1)),' YUnits'];
  else
    tipText = 'Cannot calculate gradient here.';
  end
  bkgColor = [1 1 .5]; % May be removed.
  fontSize = 12; % May be removed.
  set(datatipH,'StringFcn',(@(~,~) tipText),'Orientation',...
    orientation,'backGroundColor',bkgColor,'FontSize',...
    fontSize,'Draggable','on');            % Only set text and orientation needed.    
  datatipTextBoxH=get(datatipH,'TextBoxHandle');  % May be removed.
  uistack(datatipH,'top'); % Unfortunately makes no effect, since the ylabel handles is not at the axes children list
  datatipTextBoxH=get(datatipH,'TextBoxHandle');
  set(datatipTextBoxH,'HorizontalAlignment','left',...
    'VerticalAlignment','top','Margin',0.02,'Interpreter',...
    'tex','FontName','Courier','FontSize',fontSize); % May be removed.
end
uistack(get(gca,'YLabel'),'bottom') % Also makes no effect, for the same reason.
我试过了:
更新:实施@horchler'解决方案后,出现了一个新问题:当缩放和平移轴时,轴标签也会移动.我找到了一个小修复,我改变了以下几个方面:
localAxisUpdate了获取旧ylabel属性的函数,将其替换为新的属性,并重置所有可设置的属性,但重置ylabel位置.为此,我使用了这个参考结果代码如下:
function test
  gradientStep = 1e-1;
  x=-100:gradientStep:100; xSize=numel(x);
  y=x.^3-x.^2;
  figH=figure(42);
  lineH=plot(x,y);
  ylabel('YLabel (YUnits)','FontSize',16)
  xlabel('XLabel (XUnits)','FontSize',16)
  dcH=datacursormode(figH);
  %nTips = 20;
  %for pos = round(linspace(2,xSize,nTips))
    pos = find(x>-78,1);
    datatipH=dcH.createDatatip(lineH,...
      struct('Position',[x(pos) y(pos) 1]));
    orientation = 'top-left';
    if pos>1
      tipText{1} = 'The grandient here is: ';
      tipText{2} = ['\Deltax:',sprintf('%d',x(pos)-x(pos-1)),' XUnits'];
      tipText{3} = ['\Deltay:',sprintf('%d',y(pos)-y(pos-1)),' YUnits'];
    else
      tipText = 'Cannot calculate gradient here.';
    end
    bkgColor = [1 1 .5]; % Light Yellow
    fontSize = 12;
    set(datatipH,'StringFcn',(@(~,~) tipText),'Orientation',...
      orientation,'backGroundColor',bkgColor,'FontSize',...
      fontSize,'Draggable','on');
    datatipTextBoxH=get(datatipH,'TextBoxHandle');
    datatipTextBoxH=get(datatipH,'TextBoxHandle');
    set(datatipTextBoxH,'HorizontalAlignment','left',...
      'VerticalAlignment','top','Margin',0.02,'Interpreter',...
  %end
  % Set changes due to zoom and pan to also use adaptativeDateTicks:         
  set(zoom(figH),'ActionPostCallback',...
    @(~,~) localAxisUpdate(gca));
  set(pan(figH),'ActionPostCallback',...
    @(~,~) localAxisUpdate(gca));
end
function localAxisUpdate(aH)    
  % Fix axis label on top of datatip:
  ylh = get(aH,'YLabel');
  % Get original YLabel properties
  ylstruct = get(ylh);
  % Get settable fields:
  yfieldnames=fieldnames(rmfield(set(ylh),'Position'))';
  % Remove old label:
  delete(ylh)
  % Create new one:
  ylh = ylabel(aH,'Dummy');
  % Send it bottom:
  ylpos = get(ylh,'Position');
  set(ylh, 'Position', [ylpos(1:2) 0]);
  % Reset new ylabel to old values:
  for field=yfieldnames
    field = field{1};
    set(ylh,field,ylstruct.(field));
  end
end
这种方法会产生一种不必要的效果,即ylabel将在图形上移动,直到鼠标按钮被释放.如何消除这种不良影响?
我认为解决方案可能或多或少,因为在未填充的matlab解决方案中用于更新轴刻度,但现在我需要监听ylabel postset属性.有谁知道怎么做?如果您是Windows用户,您也可以尝试提供帮助,我需要的是在图形上进行更改(平移,缩放或其他)后重置ylabel的位置.
如何通过它的句柄显式设置y标签的z位置?如果我把它放在你的循环后它似乎在R2012b中工作:
ylh = get(gca,'Ylabel')
ylpos = get(ylh,'Position');
set(ylh,'Position',[ylpos(1:2) 0]);
如果我调整z位置,我可以弹出y标签,甚至在数据提示之间交错.我不完全确定这是一个错误还是一个功能,但有时会有一些解决方法来解决涉及稍微调整元素位置的问题,以便让Matlab重新计算并重新绘制数字.
使用两个linkaxes 的解决方法,在缩放/平移多个绘图以及绘图的可见性时非常有用。
这给出了您的第一个代码(不是编辑后的代码):
gradientStep = 1e-1;   
x=-100:gradientStep:100; xSize=numel(x);
y=x.^3-x.^2;   
figH=figure(42);
plot(x,y);   
ylabel('YLabel (YUnits)','FontSize',16)
xlabel('XLabel (XUnits)','FontSize',16)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% modification starts
hax_1 = gca;
hax_2 = axes('Position', get(hax_1,'Position'));
lineH = plot(x,y);
linkaxes([hax_1 hax_2],'xy');
set(hax_2,'Visible', 'off');
% modification ends
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dcH=datacursormode(figH); 
nTips = 20; % May change the loop for a datatip at x=-78.
for pos = round(linspace(2,xSize,nTips))
  datatipH=dcH.createDatatip(lineH,struct('Position',[x(pos) y(pos)]));
  orientation = 'top-left';
  if pos>1
    tipText{1} = 'The grandient here is: ';
    tipText{2} = ['\Deltax:',sprintf('%d',x(pos)-x(pos-1)),' XUnits'];
    tipText{3} = ['\Deltay:',sprintf('%d',y(pos)-y(pos-1)),' YUnits'];
  else
    tipText = 'Cannot calculate gradient here.';
  end
  bkgColor = [1 1 .5]; % May be removed.
  fontSize = 12; % May be removed.
  set(datatipH,'StringFcn',(@(~,~) tipText),'Orientation',orientation,'backGroundColor',bkgColor,'FontSize',fontSize,'Draggable','on');  % Only set text and orientation needed.    
  datatipTextBoxH=get(datatipH,'TextBoxHandle');
  set(datatipTextBoxH,'HorizontalAlignment','left','VerticalAlignment','top','Margin',0.02,'Interpreter','tex','FontName','Courier','FontSize',fontSize); % May be removed.   
end
我使用的是 OSX 10.8.4,R2012b,并且遇到了与您相同的问题。此处,建议的解决方案在轴标签上方绘制数据提示,并允许缩放/平移,而无需使用 matlab 的未记录功能。