此问题仅适用于 unix matlab,Windows 用户将无法重现该问题。
我在尝试创建位于 y 轴标签顶部的数据提示时遇到问题。下图很能说明问题:
正如您所看到的,在 ylabel 附近创建的数据提示将到达 ylabel 文本的底部,而期望的效果是相反的:数据提示位于轴标签的顶部.
我使用以下(不是那么简单)代码生成了绘图,如下所示。您可以删除注释的行% may be removed
,或者甚至只是在 -78 上放置一个数据提示而不是循环,以实现更快的测试脚本,但如果有一天有人希望它创建自定义数据提示,我会保留此代码(在这种情况下,请考虑同时查看http://undocumentedmatlab.com/blog/controlling-plot-data-tips/ 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 句柄不在轴子句柄中)。
Update:实施@horchler'解决方案后,出现了一个新问题:缩放和平移轴时,轴标签也会移动。我找到了一个小解决方案,我更改了以下方面:
- 将 datatip z 值设置为 1,以便它始终高于 ylabel 轴 z。
- 在发生平移或缩放移动后重新创建 ylabel。为此,我实施了
localAxisUpdate
函数获取旧的 ylabel 属性,用新的属性替换它,并且重置除 ylabel 位置之外的所有可设置属性。为此我用了这个参考 https://stackoverflow.com/questions/5448161/how-to-check-if-value-is-valid-property-in-matlab
结果代码如下:
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 解决方案 http://undocumentedmatlab.com/blog/setting-axes-tick-labels-format/,但现在我需要 ylabel postset 属性的侦听器。有谁知道该怎么做?如果你是windows用户,也可以尝试帮忙,我所需要的只是在对图形进行更改(平移、缩放或其他)后重置 ylabel 的位置.