Documentation Center

  • Trial Software
  • Product Updates

stem

Plot discrete sequence data

Syntax

Description

example

stem(Y) plots the data sequence, Y, as stems that extend from a baseline along the x-axis. The data values are indicated by circles terminating each stem.

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

  • If Y is a matrix, then stem plots all elements in a row against the same x value, and the x-axis scale ranges from 1 to the number of rows in Y.

example

stem(X,Y) plots the data sequence, Y, at values specified by X. The X and Y inputs must be vectors or matrices of the same size. Additionally, X can be a row or column vector and Y must be a matrix with length(X) rows.

  • If X and Y are both vectors, then stem plots entries in Y against corresponding entries in X.

  • If X is a vector and Y is a matrix, then stem plots each column of Y against the set of values specified by X, such that all elements in a row of Y are plotted against the same value.

  • If X and Y are both matrices, then stem plots columns of Y against corresponding columns of X.

example

stem(___,'fill') fills the circles. Use this option with any of the input argument combinations in the previous syntaxes.

example

stem(___,LineSpec) specifies the line style, marker symbol, and color.

example

stem(___,Name,Value) specifies stemseries properties using one or more Name,Value pair arguments.

example

stem(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 argument combinations in the previous syntaxes.

example

h = stem(___) returns a vector of stemseries object handles in h. When multiple stemseries objects are present, you can make changes to properties of a specific stemseries by specifying a particular handle.

Examples

expand all

Plot Single Data Series

Create a stem plot of 50 data values between $-2\pi$ and $2\pi$ .

figure
Y = linspace(-2*pi,2*pi,50);
stem(Y)

Data values are plotted as stems extending from the baseline and terminating at the data value. The length of Y automatically determines the position of each stem on the x-axis.

Plot Multiple Data Series

Plot two data series using a two-column matrix.

figure
X = linspace(0,2*pi,50)';
Y = [cos(X), 0.5*sin(X)];
stem(Y)

Each column of Y is plotted as a separate series, and entries in the same row of Y are plotted against the same x value. The number of rows in Y automatically generates the position of each stem on the x-axis.

Plot Single Data Series at Specified x values

Plot 50 data values of cosine evaluated between 0 and $2\pi$ and specify the set of x values for the stem plot.

figure
X = linspace(0,2*pi,50)';
Y = cos(X);
stem(X,Y)

The first vector input determines the position of each stem on the x-axis.

Plot Multiple Data Series at Specified x values

Plot 50 data values of sine and cosine evaluated between 0 and $2\pi$ and specify the set of x values for the stem plot.

figure
X = linspace(0,2*pi,50)';
Y = [cos(X), 0.5*sin(X)];
stem(X,Y)

The vector input determines the x-axis positions for both data series.

Plot Multiple Data Series at Unique Sets of x values

Plot 50 data values of sine and cosine evaluated at different sets of x values. Specify the corresponding sets of x values for each series.

figure
x1 = linspace(0,2*pi,50)';
x2 = linspace(pi,3*pi,50)';
X = [x1, x2];
Y = [cos(x1), 0.5*sin(x2)];
stem(X,Y)

Each column of X is plotted against the corresponding column of Y.

Fill in Plot Markers

Create a stem plot and fill in the circles that terminate each stem.

figure
X = linspace(0,10,20)';
Y = (exp(0.25*X));
stem(X,Y,'fill')

Specify Stem and Marker Options

Create a stem plot and set the line style to a dotted line, the marker symbols to diamonds, and the color to red using the LineSpec option.

figure
X = linspace(0,2*pi,50)';
Y = (exp(X).*sin(X));
stem(X,Y,':diamondr')

To color the inside of the diamonds, use the 'fill' option.

Specify Additional Stem and Marker Options

Create a stem plot and set the line style to a dot-dashed line, the marker face color to red, and the marker edge color to green using Name,Value pair arguments.

figure
X = linspace(0,2*pi,25)';
Y = (cos(2*X));
stem(X,Y,'LineStyle','-.',...
     'MarkerFaceColor','red',...
     'MarkerEdgeColor','green')

The stem remains the default color.

Specify Axes for Stem Plot

Create a figure with two subplots and return the handles to each axes, s(1) and s(2). Create a stem plot in the lower subplot by referring to its axes handle, s(2).

figure
s(1) = subplot(2,1,1);
s(2) = subplot(2,1,2);

X = 0:25;
Y = [exp(0.1*X); -exp(.05*X)]';
stem(s(2),X,Y)

Return Stemseries Handle

Create a stem plot of multiple data series and return the handle to each stemseries.

figure
X = 0:25;
Y = [cos(X); exp(0.05*X)]';
h = stem(X,Y);

The stem function creates two stemseries objects, one for each column of data. The output argument, h, contains both stemseries handles.

Change the markers of the first series to blue circles and the second series to green squares using the handles.

set(h(1),'MarkerFaceColor','blue')
set(h(2),'MarkerFaceColor','green','Marker','square')

Adjust Baseline Properties

Create a stem plot and change properties of the baseline.

figure
X = linspace(0,2*pi,50)';
Y = (exp(0.3*X).*sin(3*X));
h = stem(X,Y);

Get the baseline handle, hbase. Adjust the style of the baseline by setting its LineStyle property.

hbase = get(h,'Baseline');
set(hbase,'LineStyle','--')

Make the baseline invisible by setting its Visible property to 'off' .

set(hbase,'Visible','off')

Change the Baseline Level

Create a stem plot and change the baseline level to 2.

figure
X = linspace(0,2*pi,50)';
Y = (exp(0.3*X).*sin(3*X));
h = stem(X,Y);
set(h,'BaseValue',2);

Input Arguments

expand all

Y — Data sequence to displayvector or matrix

Data sequence to display, specified as a vector or matrix. When Y is a vector, stem creates one stemseries. When Y is a matrix, stem creates a separate stemseries for each column.

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

X — Locations to plot data values in Y vector or matrix

Locations to plot data values in Y, specified as a vector or matrix. When Y is a vector, X must be a vector of the same size. When Y is a matrix, X must be a matrix of the same size, or a vector whose length equals the number of rows in Y.

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. For more information on line style, marker symbol, and color options see LineSpec.

Example: ':*r'

Data Types: char

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: 'LineStyle',':','MarkerFaceColor','red' plots the stem as a dotted line and colors the marker face red.

For more information on these properties see stemseries.

'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. The scalar sets the width size in points of the stem and marker edge.

Example: 'LineWidth',0.75

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

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

Marker symbol, specified as the comma-separated pair consisting of 'Marker' and a marker specifier. 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 same color as marker edge color

'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 — Stemseries object handlescalar or column vector

Stemseries object handle, returned as a scalar or column vector. This is a unique identifier, which you can use to query and modify the properties of a specific stemseries.

See Also

| | | |

Was this topic helpful?