MATLAB Answers

How to ensure text on plot always in front?

143 views (last 30 days)
Xiaohan Du
Xiaohan Du on 29 Aug 2017
Commented: Xiaohan Du on 29 Aug 2017
Hi all,
I plot some surfaces and label some points on the surface with it's value of z-axis, as shown on the figure. You can see that the black texts on figure 3-4 are all covered by the surfaces. How can I make sure that the black texts are always in front such that they can be seen?
Many thanks!

  4 Comments

Show 1 older comment
Xiaohan Du
Xiaohan Du on 29 Aug 2017
Does it work on 3d plot? I tried it and the text is still behind the surface, nothing changed. This is what I did:
b = text(locx, locy, locz, textToPlot, ...
'color', '[0 0 0]', 'Fontsize', 12);
uistack(b, 'top')
José-Luis
José-Luis on 29 Aug 2017
A workaround would be to make your shape a bit transparent. Modify the Alpha property. Otherwise, you could try making your text an annotation.
José-Luis
José-Luis on 29 Aug 2017
Also, it might be a rendering issue. Have you tried saving to file and looking there.
Testing different renderers might also help.

Sign in to comment.

Accepted Answer

Teja Muppirala
Teja Muppirala on 29 Aug 2017
One workaround would be to create an invisible axes on top, and then put the text in there.
figure
ax1 = axes;
mesh(ax1,peaks)
colorbar;
t(1) = text(25,25,1,'My text'); % The text will be obscured by the mesh
t(2) = text(10, 30,-2,'More text');
axHidden = axes('Visible','off','hittest','off'); % Invisible axes
linkprop([ax1 axHidden],{'CameraPosition' 'XLim' 'YLim' 'ZLim' 'Position'}); % The axes should stay aligned
set(t,'Parent',axHidden); % Put the text in the invisible Axes

  2 Comments

Adam
Adam on 29 Aug 2017
+1
The last 3 lines are very instructive for numerous similar purposes. I created a solution to one of my problems by adding a second invisible axes on top of my original, but I spent ages messing around with problems caused by zooming, panning, adding a colourbar etc.
This method still causes trouble if you mess about with colourbars on and off, but for zooming and panning it solves all those problems.
Xiaohan Du
Xiaohan Du on 29 Aug 2017
Hi both,
This is quite complex compare with just making the surfaces transparent, I tried Jose's solution with modifying the Alpha property and it's like this:
I think it's much better.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by