title: Put datatip stack on top of axis label and update axes label after a change was done on axes position
body: <p><strong>This issue is only for unix matlabs, windows users won't be able to reproduce it.</strong></p>
  
  <p>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:</p>
  
  <p><img src="https://i.stack.imgur.com/Jn2Mp.png" alt="Issue Example"> </p>
  
  <p>As you can see, the datatips created close to the ylabel will get bottom to the ylabel text, <strong>while the desire effect is the opposite: the datatip to be on top of the axis label</strong>.</p>
  
  <p>I generated the plot with the following (not so minimal) code, which is available bellow. You may remove the lines commented with <code>% may be removed</code>, 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 <a href="http://undocumentedmatlab.com/blog/controlling-plot-data-tips/" rel="nofollow noreferrer" title="Undocummented Matlab: Datatips">http://undocumentedmatlab.com/blog/controlling-plot-data-tips/</a>):   </p>
  
  <pre><code>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&gt;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.
  </code></pre>
  
  <p>I have tried:</p>
  
  <ul>
  <li>uistack all datatips to top,</li>
  <li>uistack the label to bottom (both of them don't work because the ylabel handle is not in the axes children handles).</li>
  </ul>
  
  <hr>
  
  <p><strong>Update</strong>: 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:</p>
  
  <ul>
  <li>Set datatip z-value to 1, so that it will always be higher than ylabel axis z.</li>
  <li>Recreating the ylabel afterwards a pan or zoom movement occurs. For this, I implemented <code>localAxisUpdate</code> 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 <a href="https://stackoverflow.com/questions/5448161/how-to-check-if-value-is-valid-property-in-matlab" title="How to get only set-valid properties">reference</a></li>
  </ul>
  
  <p>The resulting code is as follows: </p>
  
  <pre><code>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&gt;-78,1);
      datatipH=dcH.createDatatip(lineH,...
        struct('Position',[x(pos) y(pos) 1]));
  
      orientation = 'top-left';
  
      if pos&gt;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
  </code></pre>
  
  <p>This approach creates an unwanted effect, which is the ylabel will move across the figure until the mouse button is released. <strong>How can I remove this unwanted effect ?</strong> </p>
  
  <p>I think the solution may be more or less as it was done in <a href="http://undocumentedmatlab.com/blog/setting-axes-tick-labels-format/" rel="nofollow noreferrer" title="undocummented matlab solution for updating axes ticks">undocummented matlab solution for updating axes ticks</a>, 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, <strong>all I need is to reset the position of the ylabel after a change (pan, zoom or whatever) is made on the figure</strong>.</p>