Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

line

Create primitive line

Syntax

line(x,y)
line(x,y,z)
line
line('XData',x,'YData',y)
line('XData',x,'YData',y,'ZData',z)
line(___,Name,Value)
line(ax,___)
pl = line(___)

Description

example

line(x,y) plots a line in the current axes using the data in vectors x and y. If either x or y, or both are matrices, then line draws multiple lines. The function cycles through colors and line styles based on the ColorOrder and LineStyleOrder properties of the axes. Unlike the plot function, line adds the line to the current axes without deleting other graphics objects or resetting axes properties.

example

line(x,y,z) plots a line in three-dimensional coordinates.

line draws a line from the point (0,0) to (1,1) with the default property settings.

example

line('XData',x,'YData',y) is the low-level version of line(x,y). The behavior is the same as line(x,y), except that the line has the default line color. Also, you cannot set x or y to matrix data.

line('XData',x,'YData',y,'ZData',z) is the low-level version of line(x,y,z). The behavior is the same as line(x,y,z), except that the line has the default line color. Also, you cannot set x, y, or z to matrix data.

example

line(___,Name,Value) modifies the appearance of the line using one or more name-value argument pairs. For example, 'LineWidth',3 sets the line width to 3 points. Specify name-value pairs after all other input arguments.

line(ax,___) creates the line in the axes specified by ax instead of in the current axes (gca). Specify ax as the first input argument.

example

pl = line(___) returns all primitive Line objects created. Use pl to modify properties of a specific Line object after it is created. For a list, see Line Properties.

Examples

collapse all

Create x and y as vectors. Then plot y versus x.

x = linspace(0,10);
y = sin(x);
line(x,y)

Plot two lines by specifying x and y as matrices. Use line to plot columns of y versus columns of x as separate lines.

x = linspace(0,10)';
y = [sin(x) cos(x)];
line(x,y)

Plot a line in 3-D coordinates by specifying x, y, and z values. Change the axes to a 3-D view using view(3).

t = linspace(0,10*pi,200);
x = sin(t);
y = cos(t);
z = t;
line(x,y,z)
view(3)

Create x and y as vectors. Then plot y versus x using the low-level version of the line function.

x = linspace(0,10);
y = sin(x);
line('XData',x,'YData',y)

Draw a red, dashed line between the points (1,2) and (9,12). Set the Color and LineStyle properties as name-value pairs.

x = [1 9];
y = [2 12];
line(x,y,'Color','red','LineStyle','--')

First, draw a line from the point (3,15) to (2,12) and return the Line object. Then change the line to a green, dashed line.

Note: Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

x = [3 2];
y = [15 12];
pl = line(x,y);

pl.Color = 'green';
pl.LineStyle = '--';

Input Arguments

collapse all

x values, specified as a vector or a matrix.

Example: x = linspace(0,10,25)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

y values, specified as a vector or a matrix:

  • If x and y are both vectors with the same length, then line plots a single line.

  • If x and y are matrices with the same size, then line plots multiple lines. The function plots columns of y versus x.

  • If one of x or y is a vector and the other is a matrix, then line plots multiple lines. The length of the vector must equal one of the matrix dimensions:

    • If the vector length equals the number of matrix rows, then line plots each matrix column versus the vector.

    • If the vector length equals the number of matrix columns, then line plots each matrix row versus the vector.

    • If the matrix is square, then line plots each column versus the vector.

Example: y = sin(x)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

z values, specified as a vector or a matrix:

  • If x, y, and z are all vectors with the same length, then line plots a single line.

  • If x, y, and z are all matrices with the same size, then line plots multiple lines using the matrix columns.

  • If one or two of x, y, and z is a vector and the others are matrices of the same size, then line plots multiple lines. The length of the vector must equal one of the matrix dimensions.

Example: z = sin(x) + cos(y)

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

Target axes, specified as an Axes object or a PolarAxes object. If you do not specify the axes, then the line function plots in the current axes.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: line(x,y,'Color','red','LineWidth',3) creates a red line that is 3 points wide.

The properties listed here are only a subset. For a full list, see Line Properties.

collapse all

Line color, specified as an RGB triplet or one of the color options listed in the table.

For a custom color, specify an RGB triplet. An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range [0,1]; for example, [0.4 0.6 0.7]. Alternatively, you can specify some common colors by name. This table lists the long and short color name options and the equivalent RGB triplet values.

OptionDescriptionEquivalent RGB Triplet
'red' or 'r'Red[1 0 0]
'green' or 'g'Green[0 1 0]
'blue' or 'b'Blue[0 0 1]
'yellow' or 'y'Yellow[1 1 0]
'magenta' or 'm'Magenta[1 0 1]
'cyan' or 'c'Cyan[0 1 1]
'white' or 'w'White[1 1 1]
'black' or 'k'Black[0 0 0]
'none'No colorNot applicable

Example: line(x,y,'Color','blue')

Example: line(x,y,'Color',[0.5 0.5 1])

Line style, specified as one of the line styles listed in this table.

Line StyleDescriptionResulting Line
'-'Solid line

'--'Dashed line

':'Dotted line

'-.'Dash-dotted line

'none'No lineNo line

Line width, specified as a positive value in points. If the line has markers, then the line width also affects the marker edges.

Example: 0.75

Marker symbol, specified as one of the values listed in this table. By default, the object does not display markers. Specifying a marker symbol adds markers at each data point or vertex.

ValueDescription
'o'Circle
'+'Plus sign
'*'Asterisk
'.'Point
'x'Cross
'square' or 's'Square
'diamond' or 'd'Diamond
'^'Upward-pointing triangle
'v'Downward-pointing triangle
'>'Right-pointing triangle
'<'Left-pointing triangle
'pentagram' or 'p'Five-pointed star (pentagram)
'hexagram' or 'h'Six-pointed star (hexagram)
'none'No markers

Marker size, specified as a positive value in points.

Example: 10

Output Arguments

collapse all

Primitive line object. Use pl to query or modify properties of the line after it is created. For a list, see Line Properties.

Tips

  • Unlike the plot function, the line function does not call newplot before plotting and does not respect the value of the NextPlot property for the figure or axes. It simply adds the line to the current axes without deleting other graphics objects or resetting axes properties. However, some axes properties, such as the axis limits, can update to accommodate the line.

Introduced before R2006a

Was this topic helpful?