This problem applies only to unix layouts; Windows users will not be able to reproduce it.
I am having problems trying to create datatips that are above the y axis label. The following figure shows the problem:

As you can see, the data created next to the label will receive the bottom of the label text , while the desire effect is the opposite: the data type must be on top of the label label .
I generated a graph with the following (not very minimal) code, which is available below. You can delete the lines commented out with % may be removed , or even just put the datatip at -78 instead of a loop to achieve faster testing of the script, but I leave this code if someone once wants them to create user data ( in this case, consider 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.
I tried:
- uistack all datatips to top,
- remove the label from the bottom (both of them do not work, because the ylabel handle is not in the handles with the axles).
Update . After introducing the @horchler solution, a new problem appeared: when scaling and panning the axes, the axis label will also move. I found a small fix for this, I changed the following aspects:
- Set datatip z-value to 1 so that it is always above the ylabel z axis.
- Recreating the shortcut after this moves the pan or zoom. To do this, I implemented the
localAxisUpdate function, which receives the old ylabel properties, replaces it with a new one, and reset - all settable properties, but the ylabel position. For this, I used the link
The resulting code is as follows:
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
This approach creates an undesirable effect that the iglabel will move around the figure until the mouse button is released. How to remove this unwanted effect?
I think that the solution can be more or less, as it was done in the undocummented matlab to update the tick axes , but now I need a listener for the postet ylabel property. Does anyone know how to do this? If you are a Windows user, you can also try to help, all I need to do is reset the ylabel position after changing (pan, zoom, or something else) in the picture .