此问题仅适用于 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.
我试过了:
- uistack 所有数据提示到顶部,
- uistack 标签到底部(它们都不起作用,因为 ylabel 句柄不在轴子句柄中)。
更新:在实施@horchler 的解决方案后,出现了一个新问题:缩放和平移坐标轴时,坐标轴标签也会移动。我找到了一个小修复,我改变了以下方面:
- 将 datatip z-value 设置为 1,使其始终高于 ylabel 轴 z。
- 之后重新创建 ylabel 会发生平移或缩放移动。为此,我实现
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 用户,您也可以尝试提供帮助,我所需要的只是在对 figure 进行更改(平移、缩放或其他)后重置 ylabel 的位置。