yline

Draws a horizontal line at a given y value across the axes.

Category: Graphics > Formatting and Annotation

Syntax

yline(yvalue)

Draws a horizontal line at yvalue. A vector draws one line per element.

yline(yvalue, LineSpec)

Sets the line style and color, for example '--r'.

yline(yvalue, LineSpec, label)

Also attaches a text label to the line.

yl = yline(___)

Returns the line object.

Description

Unlike line, the line spans the whole width of the axes even when the limits change, because it is tied to a value and not to a pair of end points. It does not affect the automatic axis limits.

It is handy for marking a threshold, a target level or a mean. The vertical counterpart is xline.

Input arguments

yvalue
Position or positions along the y-axis.
label
Text shown next to the line.

Output arguments

yl
Constant line object, class matlab.graphics.chart.decoration.ConstantLine, with the properties Value, Label and Color.

Example

Mark the mean

y = [3 5 4 6 2 7];
plot(y)
h = yline(mean(y), ':r', 'mean');
h.Value
h.Label
Output
ans = 4.5000
ans = 'mean'

See also

xline yregion line ylim text