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.


Create line object


h = line(...)


For a list of properties, see Primitive Line Properties.


line creates a line object in the current axes with default values x = [0 1] and y = [0 1]. You can specify the color, width, line style, and marker type, as well as other characteristics.

The line function has two forms:

  • Automatic color and line style cycling. When you specify multiple line coordinate data as a column array using the informal syntax (i.e., the first three arguments are interpreted as the coordinates),


    MATLAB® cycles through the axes ColorOrder and LineStyleOrder property values the way the plot function does. However, unlike plot, line does not call the newplot function.

  • Purely low-level behavior. When you call line with only property name/property value pairs,


    MATLAB draws a line object in the current axes using the default line color (see the colordef function for information on color defaults). Note that you cannot specify matrix coordinate data with the low-level form of the line function.

line(X,Y) adds the line defined in vectors X and Y to the current axes. If X and Y are matrices of the same size, line draws one line per column. The values in X and Y can be numeric or datetime or duration values.

line(X,Y,Z) creates lines in three-dimensional coordinates. The values in X, Y, and Z can be numeric or datetime or duration values.

line(X,Y,Z,'PropertyName',propertyvalue,...) creates a line using the values for the property name/property value pairs specified and default values for all other properties. For a description of the properties, see Primitive Line Properties.

line('XData',x,'YData',y,'ZData',z,...) creates a line in the current axes using the property values defined as arguments. This is the low-level form of the line function, which does not accept matrix coordinate data as the other informal forms described above. The values in x, y, and z can be numeric or datetime or duration values.

line(ax,...) creates the line in the axes specified by ax instead of in the current axes (gca).

h = line(...) returns a column vector of primitive line handles corresponding to each line object the function creates.


This example uses the line function to add a shadow to plotted data. First, plot some data and save the line's handle:

t = 0:pi/20:2*pi;
hline1 = plot(t,sin(t),'k');
ax = gca;

Next, add a shadow by offsetting the x-coordinates. Make the shadow line light gray and wider than the default LineWidth:

hline2 = line(t+.06,sin(t),...
   'Color',[.8 .8 .8],...

Finally, pull the first line to the front:

set(gca,'Children',[hline1 hline2])

Drawing Lines Interactively

You can use the ginput function to select points from a figure. For example:

[x,y] = ginput(5);

Drawing with mouse motion

You can use the axes CurrentPoint property and the figure WindowButtonDownFcn and WindowButtonMotionFcn properties to select a point with a mouse click and draw a line to another point by dragging the mouse, like a simple drawing program. The following example illustrates a few useful techniques for doing this type of interactive drawing.

Click to view in editor — This example enables you to click and drag the cursor to draw lines.

Click to run example — Click the left mouse button in the axes and move the cursor, left-click to define the line end point, right-click to end drawing mode.

Input Argument Dimensions — Informal Form

This statement reuses the one-column matrix specified for ZData to produce two lines, each having four points.


If all the data has the same number of columns and one row each, MATLAB transposes the matrices to produce data for plotting. For example,


is changed to


This also applies to the case when just one or two matrices have one row. For example, the statement


is equivalent to


More About

collapse all


In its informal form, the line function interprets the first three arguments (two for 2-D) as the X, Y, and Z coordinate data, allowing you to omit the property names. You must specify all other properties as name/value pairs. For example,


The low-level form of the line function can have arguments that are only property name/property value pairs. For example,


Line properties control various aspects of the line object. You can also set and query property values after creating the line using dot notation or the set and get functions

Unlike high-level functions such as plot, line does not respect the settings of the figure and axes NextPlot properties. It simply adds line objects to the current axes. However, axes properties that are under automatic control, such as the axis limits, can change to accommodate the line within the current axes.

Connecting the dots

The coordinate data is interpreted as vectors of corresponding x, y, and z values:

X = [x(1) x(2) x(3)...x(n)]
Y = [y(1) y(2) y(3)...y(n)]
Z = [z(1) z(2) z(3)...z(n)]

where a point is determined by the corresponding vector elements:


For example, to draw a line from the point located at x = .3 and y = .4 and z = 1 to the point located at x = .7 and y = .9 and z = 1, use the following data:

axis([0 1 0 1])
line([.3 .7],[.4 .9],[1 1],'Marker','.','LineStyle','-')

See Also



Introduced before R2006a

Was this topic helpful?