Bring text to the front of a plot
Asked Answered
R

1

5

I have a figure in MATLAB. Then I add a text to it by typing,

b = text(0.5, 0.5, 'Detector action', 'Rotation', -70, 'FontSize', 25);

But the text goes behind the figure (See below), enter image description here

I also tried,

uistack(b, 'top');

but it didn't work.

Richardo answered 20/12, 2018 at 17:31 Comment(10)
Did you try it with annotation?Baptism
@Baptism No, How?Richardo
You are placing your text object in a 3D domain, so depending on your viewpoint there is always a case where it will be behind the graph. Just find the coordinate set (x,y,z) which place your text in front of the graph in the final viewAngelicaangelico
@Angelicaangelico What do you mean?Richardo
I mean the text object is placed in the same 3D domain as the data you plot, so the relative position of your patch and your text matter. It might be better to use annotation object as @Baptism suggested because they will always be on top of the axes, so on top of anything plotted in the figure.Angelicaangelico
Yes, but the problem with annotation is that it doesn't accept rotation argument.Richardo
How can I rotate the text generated by annotation?Richardo
@Baptism How can I rotate my text?Richardo
@Richardo Use an annotation of type TextArrow, which has a TextRotation property (instead of TextBox, which doesn't) and make the arrow invisible (set (LineStyle and HeadStyle) and/or Color to none).Baptism
@Baptism Oh great! Thanks.Richardo
B
7

The easiest way is not to bother with a text at all, but instead use an annotation, since such an object will be (at least by default) above the axes (and thus, anything plotted inside them).

The trick with annotation objects, is that counter-intuitively, we need not use a TextBox, but instead a TextArrow, while making the arrow itself invisible.

For example:

figure(); membrane(); 
annotation('TextArrow', [.5 .5], [.5 .5], 'String','Some text', 'TextRotation', -30, ...
           'HeadStyle','none','TextBackgroundColor','w' );

enter image description here

Baptism answered 20/12, 2018 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.