将datatip堆栈放在轴标签的顶部,并在轴位置进行更改后更新轴标签 [英] Put datatip stack on top of axis label and update axes label after a change was done on axes position

查看:77
本文介绍了将datatip堆栈放在轴标签的顶部,并在轴位置进行更改后更新轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题仅适用于unix matlab,Windows用户将无法复制它.

尝试创建位于y轴标签顶部的数据提示时遇到麻烦.下图说明了该问题:

I am having trouble while trying to create datatips in which are on top of the y axis label. The following picture illustrate the issue:

如您所见,在ylabel附近创建的数据提示将位于ylabel文本的底部,而效果则相反:数据提示位于轴标签的顶部.

As you can see, the datatips created close to the ylabel will get bottom to the ylabel text, while the desire effect is the opposite: the datatip to be on top of the axis label.

我使用以下(不是最少的)代码生成了图表,下面提供了该代码.您可以删除用% may be removed注释的行,或者甚至将datatip放在−78而不是循环上,以实现更快的测试脚本,但是如果某天有人希望它创建自定义datatips,我将保留此代码(在在这种情况下,请考虑同时查看 http://undocumentedmatlab .com/blog/controlling-plot-data-tips/):

I generated the plot with the following (not so minimal) code, which is available bellow. You may remove the lines commented with % may be removed, or even just put a datatip on −78 instead of a loop in order to achieve a faster testing script, but I leave this code if someone one day wants it to create custom datatips (in this case, consider seeing also 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.

我尝试过:

  • ui将所有数据提示堆叠到顶部,
  • 将标签堆叠到底部(这两个标签都不起作用,因为ylabel手柄不在子手柄的轴上).

更新:实施@horchler解决方案后,出现了一个新问题:在缩放和平移轴时,轴标签也会移动.我为此找到了一个小解决方案,我更改了以下几个方面:

Update: After implementing the @horchler' solution, a new issue appeared: when zooming and panning the axes, the axes label would also move. I've found a small fix for that, I changed the following aspects:

  • 将datatip z值设置为1,使其始终高于ylabel轴z.
  • 在发生平移或缩放移动之后重新创建ylabel.为此,我实现了localAxisUpdate函数,该函数获取旧的ylabel属性,将其替换为新的ylabel属性,并重置所有可设置的属性,但ylabel位置.为此,我使用了参考
  • Set datatip z-value to 1, so that it will always be higher than ylabel axis z.
  • Recreating the ylabel afterwards a pan or zoom movement occurs. For this, I implemented localAxisUpdate function that get the old ylabel properties, replace it by a new one, and them reset all settable properties but the ylabel position. For this I used this 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会在图形上移动,直到释放鼠标按钮为止. 如何消除这种不良影响?

This approach creates an unwanted effect, which is the ylabel will move across the figure until the mouse button is released. How can I remove this unwanted effect ?

我认为解决方案或多或少地与用于更新坐标轴刻度的无用matlab解决方案,但是现在我需要ylabel postset属性的侦听器.有谁知道该怎么做?如果您是Windows用户,也可以尝试提供帮助,我需要做的只是在对图形进行更改(平移,缩放或其他操作)后重置ylabel的位置. >

I think the solution may be more or less as it was done in undocummented matlab solution for updating axes ticks, but now I would need the listener to the ylabel postset property. Does anyone knows how to do that? If you are a windows user, you can also try to help, all I need is to reset the position of the ylabel after a change (pan, zoom or whatever) is made on the figure.

推荐答案

同时使用

A workaround that uses both linkaxes, so useful when zooming/panning multiple plots, and the visibilty of plots.

  1. 使用要绘制的功能创建一个轴(hax_1),没有数据提示
  2. 使用要绘制的功能和数据提示创建轴(hax_2),但不带有轴标签
  3. 将hax_2可见性设置为关闭"(这将绘制上面的数据提示第一个轴标签)
  4. 用linkaxes([hax_1 hax_2],'xy')链接两个轴; (在其中一个轴上进行缩放和平移会在第二个轴上进行即时修改)
  1. create an axes (hax_1) with the function to be plotted, without the datatips
  2. create an axes (hax_2) with the function to be plotted AND the datatips but without the axes labels
  3. set hax_2 visibility to 'off' (this will plot the datatips above the first axes labels)
  4. link the 2 axes with linkaxes([hax_1 hax_2],'xy'); (zooming and panning on one of the axes will modify on the fly the second axes)

这给出了您的第一个代码(而不是已编辑的代码):

This gives with your first code (not the edited one):

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未记录的功能的情况下进行缩放/平移.

I am on OSX 10.8.4, R2012b, and had the same issue as yours. Here, the proposed solution plots datatips above the axis labels and allow zooming/panning without making use of undocumented features of matlab.

这篇关于将datatip堆栈放在轴标签的顶部,并在轴位置进行更改后更新轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆