【发布时间】:2013-07-09 04:14:47
【问题描述】:
此问题仅适用于 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 的解决方案后,出现了一个新问题:缩放和平移坐标区时,坐标区标签也会移动。我找到了一个小修复,我改变了以下几个方面:
- 将数据提示 z 值设置为 1,使其始终高于 ylabel 轴 z。
- 之后重新创建 ylabel 会发生平移或缩放移动。为此,我实现了
localAxisUpdate函数,该函数获取旧的 ylabel 属性,将其替换为新属性,然后重置所有可设置的属性,但 ylabel 位置除外。为此我使用了这个reference
生成的代码如下:
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 将在图形上移动,直到释放鼠标按钮。 我怎样才能消除这种不良影响?
我认为解决方案可能或多或少就像在 undocummented matlab solution for updating axes ticks 中所做的那样,但现在我需要 ylabel postset 属性的侦听器。有谁知道该怎么做?如果您是 windows 用户,您也可以尝试提供帮助,我只需要在对图形进行更改(平移、缩放或其他)后重置 ylabel 的位置。 p>
【问题讨论】:
-
fyi,您的原始代码在 R2013a (WinXP) 中无需修改即可工作:i.stack.imgur.com/I9MCS.png
-
我也在使用 Matlab R2013a,但在 MacOs 10.8.4 上。添加了检查这是否真的是平台相关问题的请求。