Quantcast

Documentation Center

  • Trial Software
  • Product Updates

plot

2-D line plot

Syntax

  • plot(X,Y) example
  • plot(X,Y,LineSpec)
  • plot(X1,Y1,...,Xn,Yn) example
  • plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) example

Description

example

plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.

  • If X and Y are both vectors, then they must have equal length and MATLAB® plots Y versus X.

  • If X and Y are both matrices, then they must have equal size and MATLAB plots columns of Y versus columns of X.

  • If one of X or Y is a vector and the other is a matrix, then the matrix must have dimensions such that one of its dimensions equals the vector length. If the number of matrix rows equals the vector length, then MATLAB plots each matrix column versus the vector. If the number of matrix columns equals the vector length, then MATLAB plots each matrix row versus the vector. If the matrix is square, then MATLAB plots each column versus the vector.

  • If one of X or Y is a scalar and the other is a vector, then MATLAB plots the vector as discrete points at the scalar value.

plot(X,Y,LineSpec) sets the line style, marker symbol, and color.

example

plot(X1,Y1,...,Xn,Yn) plots multiple X, Y pairs using the same axes for all lines.

example

plot(X1,Y1,LineSpec1,...,Xn,Yn,LineSpecn) sets the line style, marker type, and color for each line. You can mix X, Y, LineSpec triplets with X, Y pairs. For example, plot(X1,Y1,X2,Y2,LineSpec2,X3,Y3).

example

plot(Y) creates a 2-D line plot of the data in Y versus the index of each value.

  • If Y is a vector, then the x-axis scale ranges from 1 to length(Y).

  • If Y is a matrix, then MATLAB plots the columns of Y versus their row number. The x-axis scale ranges from 1 to the number of rows in Y.

  • If Y is complex, then MATLAB plots the imaginary part of Y versus the real part of Y, such that plot(Y) is equivalent to plot(real(Y),imag(Y)).

plot(Y,LineSpec) sets the line style, marker symbol, and color.

example

plot(___,Name,Value) specifies lineseries properties using one or more Name,Value pair arguments. Use this option with any of the input argument combinations in the previous syntaxes. Name,Value pair settings apply to all the lines plotted. You cannot specify different Name,Value pairs for each line using this syntax.

example

plot(axes_handle,___) plots into the axes specified by axes_handle instead of into the current axes (gca). The option, axes_handle can precede any of the input combinations in the previous syntaxes.

example

h = plot(___) returns a column vector of lineseries handles, where h contains one handle per line plotted. When multiple lines are present, you can make changes to properties of a specific line by specifying its handle.

Examples

expand all

Create Line Plot

Define x as a vector of linearly spaced values between 0 and $2\pi$ . Use an increment of $\pi/100$ between the values. Define y as sine values of x.

x = 0:pi/100:2*pi;
y = sin(x);

Create a line plot of the data.

figure % opens new figure window
plot(x,y)

Plot Multiple Lines

Define x as 100 linearly spaced values between $-2\pi$ and $2\pi$ . Define y1 and y2 as sine and cosine values of x. Create a line plot of both sets of data.

x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);

figure
plot(x,y1,x,y2)

Create Line Plot From Matrix

Define Y as the 4-by-4 matrix returned by the magic function.

Y = magic(4)
Y =

    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

Create a 2-D line plot of Y. MATLAB® plots each matrix column as a separate line.

figure
plot(Y)

Specify Line Style

Plot three sine curves with a small phase shift between each line. Use the default line style for the first line. Specify a dashed line style for the second line and a dotted line style for the third line.

x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);

figure
plot(x,y1,x,y2,'--',x,y3,':')

MATLAB® cycles the line color through the default color order.

Specify Line Style, Color, and Marker

Plot three sine curves with a small phase shift between each line. Use a green line with no markers for the first sine curve. Use a blue dashed line with circle markers for the second sine curve. Use only cyan star markers for the third sine curve.

x = 0:pi/10:2*pi;
y1 = sin(x);
y2 = sin(x-0.25);
y3 = sin(x-0.5);

figure
plot(x,y1,'g',x,y2,'b--o',x,y3,'c*')

Specify Line Width, Marker Size, and Marker Color

Create a line plot and use the LineSpec option to specify a dashed green line with square markers. Use Name,Value pairs to specify the line width, marker size, and marker colors. Set the marker edge color to blue and set the marker face color using an RGB color value.

x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));

figure
plot(x,y,'--gs',...
    'LineWidth',2,...
    'MarkerSize',10,...
    'MarkerEdgeColor','b',...
    'MarkerFaceColor',[0.5,0.5,0.5])

Add Title and Axis Labels

Use the linspace function to define x as a vector of 150 values between 0 and 10. Define y as cosine values of x.

x = linspace(0,10,150);
y = cos(5*x);

Create a 2-D line plot of the cosine curve. Change the line color to a shade of blue-green using an RGB color value. Add a title and axis labels to the graph using the title, xlabel, and ylabel functions.

figure
plot(x,y,'Color',[0,0.7,0.9])

title('2-D Line Plot')
xlabel('x')
ylabel('cos(5x)')

Specify Axes for Line Plot

Create a figure with two subplots and return the handle to each subplot axes, s(1) and s(2).

figure; % new figure
s(1) = subplot(2,1,1); % top subplot
s(2) = subplot(2,1,2); % bottom subplot

Create a 2-D line plot in each axes by referring to the axes handles. Add a title and y-axis label to each axes by passing the axes handles to the title and ylabel functions.

x = linspace(0,3);
y1 = sin(5*x);
y2 = sin(15*x);

plot(s(1),x,y1)
title(s(1),'Top Subplot')
ylabel(s(1),'sin(5x)')

plot(s(2),x,y2)
title(s(2),'Bottom Subplot')
ylabel(s(2),'sin(15x)')

Change Line Properties Using Handles

Define x as 100 linearly spaced values between $-2\pi$ and $2\pi$ . Define y1 and y2 as sine and cosine values of x. Create a line plot of both sets of data and return the handles of the two lines in h.

x = linspace(-2*pi,2*pi);
y1 = sin(x);
y2 = cos(x);

figure
h = plot(x,y1,x,y2);

Use the handle of the first line, h(1), to change the line width to 2. Use the handle of the second line, h(2), to add star markers to the plotted line.

set(h(1),'LineWidth',2);
set(h(2),'Marker','*');

Input Arguments

expand all

Y — Data values to plotscalar | vector | matrix

Data values to plot, specified as a scalar, a vector, or a matrix. To plot against specific x-values you must also specify X.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical
Complex Number Support: Yes

X — Locations along the x-axis to plot data values in Yscalar | vector | matrix

Locations along the x-axis to plot data values in Y, specified as a scalar, a vector, or a matrix. If you specify both X and Y input arguments, and if either X or Y is complex, then plot ignores the imaginary components.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

LineSpec — Line style, marker symbol, and colorstring

Line style, marker symbol, and color, specified as a string. The elements of the string can appear in any order, and you can omit one or more options from the string specifier. If you omit the line style and specify the marker character, then the plot shows only the marker and no line.

If Y is a matrix and you specify a color with LineSpec, then all lines are plotted using the specified color. If you specify a marker type or line style and do not specify a color, then MATLAB cycles through the predefined color order.

Example: '--or' is a red dashed line with circle markers

SpecifierLine Style
-Solid line (default)
--Dashed line
:Dotted line
-.Dash-dot line

SpecifierMarker
oCircle
+Plus sign
*Asterisk
.Point
xCross
sSquare
dDiamond
^Upward-pointing triangle
vDownward-pointing triangle
>Right-pointing triangle
<Left-pointing triangle
pPentagram
hHexagram

SpecifierColor

y

yellow

m

magenta

c

cyan

r

red

g

green

b

blue

w

white

k

black

axes_handle — Axes handlehandle

Axes handle, which is the reference to an axes object. Use the gca function to get the handle to the current axes, for example, axes_handle = gca;.

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: 'Marker','o','MarkerFaceColor','red'

For a complete list of lineseries properties, see lineseries properties.

'Color' — Color[0 0 1] (blue) (default) | three-element RGB vector | string

Color, specified as the comma-separated pair consisting of 'Color' and a three-element RGB vector or a string containing the short or long name of the color. The RGB vector 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].

This table lists the predefined colors and their RGB equivalents.

RGB Vector

Short Name

Long Name

[1 1 0]

'y'

'yellow'

[1 0 1]

'm'

'magenta'

[0 1 1]

'c'

'cyan'

[1 0 0]

'r'

'red'

[0 1 0]

'g'

'green'

[0 0 1]

'b'

'blue'

[1 1 1]

'w'

'white'

[0 0 0]

'k'

'black'

Example: 'Color',[0 1 0]

Example: 'Color','green'

Example: 'Color','g'

'LineStyle' — Line style'-' (default) | '--' | ':' | '-.' | 'none'

Line style, specified as the comma-separated pair consisting of 'LineStyle' and a line style specifier. This table lists supported line styles.

Specifier

Line Style

'- '

Solid line (default)

'--'

Dashed line

':'

Dotted line

'-. '

Dash-dot line

'none'

No line

Example: 'LineStyle','-.'

'LineWidth' — Line width0.5 (default) | scalar

Line width, specified as the comma-separated pair consisting of 'LineWidth' and a scalar. Specify the value in points, where one point equals 1/72 inches. If any markers are displayed, then LineWidth also sets the width of the marker edges.

Example: 'LineWidth',0.75

'Marker' — Marker symbol'none' (default) | string

Marker symbol, specified as the comma-separated pair consisting of 'Marker' and a marker specifier. By default, a plot line does not have markers. You can add markers at each data point along the line by specifying a marker symbol.

This table lists supported marker symbols.

Specifier

Marker Symbol

'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 marker

Example: 'Marker','+'

Example: 'Marker','diamond'

'MarkerEdgeColor' — Marker edge color'auto' (default) | 'none' | three-element RGB vector | string

Marker edge color, specified as the comma-separated pair consisting of 'MarkerEdgeColor' and a color value. The color value can be one of the supported strings or an RGB vector, listed in the following tables.

Specifier

Result

'auto'

Uses same color as line color

'none'

Specifies no color, which makes unfilled markers invisible

For an RGB vector, use 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]. This table lists the predefined string colors and their RGB equivalents.

RGB Vector

Short Name

Long Name

[1 1 0]

'y'

'yellow'

[1 0 1]

'm'

'magenta'

[0 1 1]

'c'

'cyan'

[1 0 0]

'r'

'red'

[0 1 0]

'g'

'green'

[0 0 1]

'b'

'blue'

[1 1 1]

'w'

'white'

[0 0 0]

'k'

'black'

Example: 'MarkerEdgeColor',[1 .8 .1]

'MarkerFaceColor' — Marker face color'none' (default) | 'auto' | three-element RGB vector | string

Marker face color, specified as the comma-separated pair consisting of 'MarkerFaceColor' and a color value. MarkerFaceColor sets the fill color for markers that are closed shapes (circle, square, diamond, pentagram, hexagram, and the four triangles). The color value can be one of the supported strings or an RGB vector, listed in the following tables.

Specifier

Result

'auto'

Uses the value of the axes Color property to fill the markers. If the axes Color property is 'none', then plot uses the figure Color value instead.

'none'

Makes the interior of the marker transparent, allowing the background to show through (default).

For an RGB vector, use 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]. This table lists the predefined string colors and their RGB equivalents.

RGB Vector

Short Name

Long Name

[1 1 0]

'y'

'yellow'

[1 0 1]

'm'

'magenta'

[0 1 1]

'c'

'cyan'

[1 0 0]

'r'

'red'

[0 1 0]

'g'

'green'

[0 0 1]

'b'

'blue'

[1 1 1]

'w'

'white'

[0 0 0]

'k'

'black'

Example: 'MarkerFaceColor',[0 .8 1]

'MarkerSize' — Marker size6 (default) | scalar

Marker size, specified as the comma-separated pair consisting of 'MarkerSize' and a scalar in points.

Example: 'MarkerSize',3.75

Output Arguments

expand all

h — One or more lineseries handlesscalar | vector

One or more lineseries handles, returned as a scalar or a vector. These are unique identifiers, which you can use to query and modify properties of a specific line.

More About

expand all

Tips

  • plot automatically chooses colors and line styles in the order specified by the ColorOrder and LineStyleOrder properties of the current axes. plot cycles through the color order with the first line style. Then, plot cycles through it again with each additional line style.

    To change the color and line style orders for the current axes, set the ColorOrder and LineStyleOrder axes properties. If you do not specify these properties, then plot uses the default values. For example, to set the line style order for the current axes to a solid line with asterisk markers, a dotted line, and circle markers with no line, use this command:

    set(gca,'LineStyleOrder', '-*|:|o')
  • Data can include NaN and inf values, which cause breaks in the lines drawn. For example, this code plots the first two elements, skips the third element, and draws another line using the last two elements: plot([1,2,NaN,4,5]).

See Also

| | | | | | | |

Was this topic helpful?