Graphics |
Creating Text Annotations with the text or gtext Command
To create a text annotation using the text
function, you must specify the the text and its location in the graph, using x- and y-coordinates. You specify the coordinates in the units of the graph.
For example, the following code creates text annotations at specific points in the Lotka-Volterra Predator-Prey Population Model graph.
str1(1) = {'Many Predators;'}; str1(2) = {'Prey Population'}; str1(3) = {'Will Decline'}; text(7,220,str1) str2(1) = {'Few Predators;'}; str2(2) = {'Prey Population'}; str2(3) = {'Will Increase'}; text(5.5,125,str2)
This example also illustrates how to create multi-line text annotations with cell arrays.
Calculating the Position of Text Annotations
You can also calculate the positions of text annotations in a graph. The following code adds annotations at three data points on a graph.
text(3*pi/4,sin(3*pi/4),...
'\leftarrowsin(t) = .707',...
'FontSize',16)
text(pi,sin(pi),'\leftarrowsin(t) = 0',... 'FontSize',16) text(5*pi/4,sin(5*pi/4),'sin(t) = -.707\rightarrow',... 'HorizontalAlignment','right',... 'FontSize',16)
The HorizontalAlignment of the text string 'sin(t) =
-.707 \rightarrow'
is set to right
to place it on the left side of the point [5*pi/4,sin(5*pi/4)]
on the graph. For more information about aligning text annotations, see Text Alignment.
Defining Symbols. For information on using symbols in text strings, see Mathematical Symbols, Greek Letters, and TeX Characters.
You can use text objects to annotate axes at arbitrary locations. MATLAB locates text in the data units of the axes. For example, suppose you plot the function with A = 0.25, = 0.005, and t = 0 to 900.
To annotate the point where the value of t = 300, calculate the text coordinates using the function you are plotting.
text(300,.25*exp(-0.005*300),... '\bullet\leftarrow\fontname{times}0.25{\ite}^{-0.005{\itt}} at {\itt} = 300',... 'FontSize',14)
This statement defines the text Position
property as
The default text alignment places this point to the left of the string and centered vertically with the rectangle defined by the text Extent
property. The following section provides more information about changing the default text alignment.
Creating Text Annotations in Plot Editing Mode | Text Alignment |