Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Add Text to Specific Points on Graph

Add Text to Three Data Points on Graph

This example shows how to add text descriptions with arrows that point to three data points on a graph.

Use the linspace function to create t as a vector of 50 values between 0 and . Create y as sine values. Plot the data.

t = linspace(0,2*pi,50);
y = sin(t);
plot(t,y)

Use the text function to add a text description to the graph at the point . The first two input arguments to this function specify the text position. The third argument specifies the text. Display an arrow pointing to the left by including the TeX markup \leftarrow in the text. Use the TeX markup \pi for the Greek letter .

x1 = pi;
y1 = sin(pi);
txt1 = '\leftarrow sin(\pi) = 0';
text(x1,y1,txt1)

Add text descriptions to two more data points on the graph. By default, the data point is to the left of the text. To show the data point to the right of the text, specify the HorizontalAlignment property as 'right'. Use the TeX markup \rightarrow to display an arrow pointing to the right.

x2 = 3*pi/4;
y2 = sin(3*pi/4);
txt2 = '\leftarrow sin(3\pi/4) = 0.71';
text(x2,y2,txt2)

x3 = 5*pi/4;
y3 = sin(5*pi/4);
txt3 = 'sin(5\pi/4) = -0.71 \rightarrow';
text(x3,y3,txt3,'HorizontalAlignment','right')

Determine Minimum and Maximum Points and Add Text

This example shows how to determine the minimum and maximum data points on a graph and add text descriptions next to these values.

Create a plot.

x = linspace(-3,3);
y = (x/5-x.^3).*exp(-2*x.^2);
plot(x,y)

Find the indices of the minimum and maximum values in y. Use the indices to determine the (x,y) values at the minimum and maximum points.

indexmin = find(min(y) == y); 
xmin = x(indexmin); 
ymin = y(indexmin);

indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);

Add text to the graph at these points. Use num2str to convert the y values to text. Specify the text alignment in relation to the data point using the HorizontalAlignment property.

strmin = ['Minimum = ',num2str(ymin)];
text(xmin,ymin,strmin,'HorizontalAlignment','left');

strmax = ['Maximum = ',num2str(ymax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');

See Also

| | | | |

Related Topics

Was this topic helpful?