line

Draws a primitive line through the given points in the current axes.

Category: Graphics > Formatting and Annotation

Syntax

line(x, y)

Draws a line through the points (x(i), y(i)).

line(x, y, z)

Draws a line in 3-D.

line(___, Name, Value)

Sets properties such as 'Color', 'LineWidth' and 'LineStyle'.

h = line(___)

Returns the line object.

Description

Unlike plot, line does not clear the axes, does not reset the limits to fit data in a special way and does not advance the color order: it only adds an object to what is already there. That makes it handy for annotating a plot with a reference segment or an arrow shaft.

If there are no axes yet, one is created. Each call draws a single line, with the first coordinate vector against the second.

Input arguments

x, y, z
Coordinate vectors of the same length.
Name, Value
Line properties, for example 'Color', 'r'.

Output arguments

h
Line object with the properties XData, YData, ZData, Color and others.

Example

A segment over a plot

plot(0:5, (0:5).^2)
h = line([0 5], [0 25], 'Color', 'r', 'LineStyle', '--');
h.XData
h.YData
Output
ans = 1×2

     0     5
ans = 1×2

     0    25

See also

plot xline yline rectangle text