line
Create primitive line
Description
line
draws a line from the point (0,0)
to (1,1)
with
the default property settings.
line(___,
modifies the
appearance of the line using one or more namevalue argument pairs. For example,
Name,Value
)'LineWidth',3
sets the line width to 3
points. Specify namevalue pairs after all other input arguments. If you specify
the data using namevalue pairs, for example
line('XData',x,'YData',y)
, then you must specify vector
data.
line(
creates the line in the
Cartesian, polar, or geographic axes specified by ax
,___)ax
instead
of in the current axes (gca
). Specify ax
as the first input argument.
returns
all primitive pl
= line(___)Line
objects created. Use pl
to
modify properties of a specific Line
object after
it is created. For a list, see Line Properties.
Examples
Plot Line Using Vector Data
Create x
and y
as vectors. Then plot y
versus x
.
x = linspace(0,10); y = sin(x); line(x,y)
Plot Multiple Lines Using Matrix Data
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 Line with 3D Coordinates
Plot a line in 3D coordinates by specifying x, y, and z values. Change the axes to a 3D view using view(3)
.
t = linspace(0,10*pi,200); x = sin(t); y = cos(t); z = t; line(x,y,z) view(3)
Plot Line Using LowLevel Syntax
Create x
and y
as vectors. Then call the lowlevel version of the line
function by specifying the data as namevalue pair arguments. When you call the function this way, the resulting line is black.
x = linspace(0,10); y = sin(x); line('XData',x,'YData',y)
Specify Line Properties
Draw a red, dashed line between the points (1,2) and (9,12). Set the Color
and LineStyle
properties as namevalue pairs.
x = [1 9]; y = [2 12]; line(x,y,'Color','red','LineStyle','')
Change Line Properties After Creation
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. Use dot notation to set properties.
x = [3 2]; y = [15 12]; pl = line(x,y);
pl.Color = 'green'; pl.LineStyle = '';
Input Arguments
x
— First coordinate
vector  matrix
First coordinate, specified as a vector or a matrix. Matrix inputs are supported for Cartesian axes only.
The interpretation of the first coordinate depends on the type of axes. For Cartesian axes, the first coordinate is xaxis position in data units.
If
x
andy
are both vectors with the same length, thenline
plots a single line.If
x
andy
are matrices with the same size, thenline
plots multiple lines. The function plots columns ofy
versusx
.If one of
x
ory
is a vector and the other is a matrix, thenline
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.
For polar axes, the first coordinate is the polar angle
θ in radians. For geographic axes, the first
coordinate is latitude in degrees. To plot lines in these types of axes,
x
and y
must be the same
size.
Example: x = linspace(0,10,25)
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 categorical
 datetime
 duration
y
— Second coordinate
vector  matrix
Second coordinate, specified as a vector or a matrix. Matrix inputs are supported for Cartesian axes only.
The interpretation of the second coordinate depends on the type of axes. For Cartesian axes, the second coordinate is yaxis position in data units.
If
x
andy
are both vectors with the same length, thenline
plots a single line.If
x
andy
are matrices with the same size, thenline
plots multiple lines. The function plots columns ofy
versusx
.If one of
x
ory
is a vector and the other is a matrix, thenline
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.
For polar axes, the second coordinate is the radius in data units. For
geographic axes, the second coordinate is longitude in degrees. To plot
lines in these types of axes, x
and y
must be the same size.
Example: y = sin(x)
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 categorical
 datetime
 duration
z
— Third coordinate
vector  matrix
Third coordinate, specified as a vector or a matrix. Matrix inputs are supported for Cartesian axes only.
The interpretation of the third coordinate depends on the type of axes. For Cartesian axes, the third coordinate is zaxis position in data units.
If
x
,y
, andz
are all vectors with the same length, thenline
plots a single 3D line.If
x
,y
, andz
are all matrices with the same size, thenline
plots multiple 3D lines using the matrix columns.If one or two of
x
,y
, andz
is a vector and the others are matrices of the same size, thenline
plots multiple 3D lines. The length of the vector must equal one of the matrix dimensions.
For polar and geographic axes, the third coordinate affects the layering
of 2D lines on the axes. To use the third coordinate in these types of
axes, x
, y
, and z
must be the same size.
Example: z = sin(x) + cos(y)
Data Types: single
 double
 int8
 int16
 int32
 int64
 uint8
 uint16
 uint32
 uint64
 categorical
 datetime
 duration
ax
— Target axes
Axes
object  PolarAxes
object  GeographicAxes
object
Target axes, specified as an Axes
object, a
PolarAxes
object, or a
GeographicAxes
object. If you do not specify the
axes, then the line
function plots in the current
axes.
NameValue Arguments
Specify optional
commaseparated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
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.
Color
— Line color
[0 0.4470 0.7410]
(default)  RGB triplet  hexadecimal color code  'r'
 'g'
 'b'
 ...
Line color, specified as an RGB triplet, a hexadecimal color code, a color name, or a short name.
For a custom color, specify an RGB triplet or a hexadecimal color code.
An RGB triplet is a threeelement 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]
.A hexadecimal color code is a character vector or a string scalar that starts with a hash symbol (
#
) followed by three or six hexadecimal digits, which can range from0
toF
. The values are not case sensitive. Thus, the color codes'#FF8800'
,'#ff8800'
,'#F80'
, and'#f80'
are equivalent.
Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.
Color Name  Short Name  RGB Triplet  Hexadecimal Color Code  Appearance 

'red'  'r'  [1 0 0]  '#FF0000'  
'green'  'g'  [0 1 0]  '#00FF00'  
'blue'  'b'  [0 0 1]  '#0000FF'  
'cyan'
 'c'  [0 1 1]  '#00FFFF'  
'magenta'  'm'  [1 0 1]  '#FF00FF'  
'yellow'  'y'  [1 1 0]  '#FFFF00'  
'black'  'k'  [0 0 0]  '#000000'  
'white'  'w'  [1 1 1]  '#FFFFFF'  
'none'  Not applicable  Not applicable  Not applicable  No color 
Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB^{®} uses in many types of plots.
RGB Triplet  Hexadecimal Color Code  Appearance 

[0 0.4470 0.7410]  '#0072BD'  
[0.8500 0.3250 0.0980]  '#D95319'  
[0.9290 0.6940 0.1250]  '#EDB120'  
[0.4940 0.1840 0.5560]  '#7E2F8E'  
[0.4660 0.6740 0.1880]  '#77AC30'  
[0.3010 0.7450 0.9330]  '#4DBEEE'  
[0.6350 0.0780 0.1840]  '#A2142F' 
Example: line(x,y,'Color','blue')
Example: line(x,y,'Color',[0.5 0.5 1])
Example: line(x,y,'Color','#D9A2E9')
LineStyle
— Line style
''
(default)  ''
 ':'
 '.'
 'none'
Line style, specified as one of the options listed in this table.
Line Style  Description  Resulting Line 

''  Solid line 

''  Dashed line 

':'  Dotted line 

'.'  Dashdotted line 

'none'  No line  No line 
LineWidth
— Line width
0.5
(default)  positive value
Line width, specified as a positive value in points, where 1 point = 1/72 of an inch. If the line has markers, then the line width also affects the marker edges.
The line width cannot be thinner than the width of a pixel. If you set the line width to a value that is less than the width of a pixel on your system, the line displays as one pixel wide.
Marker
— Marker symbol
'none'
(default)  'o'
 '+'
 '*'
 '.'
 ...
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.
Marker  Description  Resulting Marker 

'o'  Circle 

'+'  Plus sign 

'*'  Asterisk 

'.'  Point 

'x'  Cross 

'_'  Horizontal line 

''  Vertical line 

's'  Square 

'd'  Diamond 

'^'  Upwardpointing triangle 

'v'  Downwardpointing triangle 

'>'  Rightpointing triangle 

'<'  Leftpointing triangle 

'p'  Pentagram 

'h'  Hexagram 

'none'  No markers  Not applicable 
MarkerSize
— Marker size
6
(default)  positive value
Marker size, specified as a positive value in points, where 1 point = 1/72 of an inch.
Output Arguments
pl
— Primitive line object
primitive line object
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, theline
function does not callnewplot
before plotting and does not respect the value of theNextPlot
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.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
Usage notes and limitations:
This function accepts GPU arrays, but does not run on a GPU.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
This function operates on distributed arrays, but executes in the client MATLAB.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
See Also
Functions
Properties
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)