stem - Plot discrete sequence data - MATLAB (original) (raw)
Plot discrete sequence data
Syntax
Description
Vector and Matrix Data
stem([Y](#btrw%5Fxi-1-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 tolength(Y)
. - If
Y
is a matrix, thenstem
plots all elements in a row against the same x value, and the _x_-axis scale ranges from 1 to the number of rows inY
.
stem([X](#btrw%5Fxi-1-X),[Y](#btrw%5Fxi-1-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
andY
are both vectors, thenstem
plots entries inY
against corresponding entries inX
. - If
X
is a vector andY
is a matrix, thenstem
plots each column ofY
against the set of values specified byX
, such that all elements in a row ofY
are plotted against the same value. - If
X
andY
are both matrices, thenstem
plots columns ofY
against corresponding columns ofX
.
stem(___,`"filled"`)
fills the circles. Use this option with any of the input argument combinations in the previous syntaxes.
stem(___,[LineSpec](#btrw%5Fxi-1%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8))
specifies the line style, marker symbol, and color.
Table Data
stem([tbl](#btrw%5Fxi-1%5Fsep%5Fmw%5F1f5b8358-45d8-4c4d-8a0d-17e2da07c841),[yvar](#mw%5F0ee057c8-e493-4a58-960a-74669863583d))
plots the specified variable from the table against the row indices of the table. If the table is a timetable, the specified variable is plotted against the row times of the timetable. To plot one set of _y_-values, specify one variable for yvar
. To plot multiple sets of_y_-values, specify multiple variables foryvar
. (since R2022b)
stem([tbl](#btrw%5Fxi-1%5Fsep%5Fmw%5F1f5b8358-45d8-4c4d-8a0d-17e2da07c841),[xvar](#mw%5F4f7f22fd-b61d-4bb8-8434-510ae53615c5),[yvar](#mw%5F0ee057c8-e493-4a58-960a-74669863583d))
plots the variables xvar
and yvar
from the table tbl
. You can specify one or multiple variables forxvar
and yvar
. If both arguments specify multiple variables, they must specify the same number of variables. (since R2022b)
Additional Options
stem(___,[Name,Value](#namevaluepairarguments))
modifies the stem chart using one or more Name,Value
pair arguments.
stem([ax](#btrw%5Fxi-1-ax),___)
plots into the axes specified by ax
instead of into the current axes (gca
). The option, ax
, can precede any of the input argument combinations in the previous syntaxes.
[h](#btrw%5Fxi-1-h) = stem(___)
returns a vector of Stem
objects in h
. Use h
to modify the stem chart after it is created.
Examples
Plot Single Data Series
Create a stem plot of 50 data values between -2π and 2π.
figure Y = linspace(-2pi,2pi,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,2pi,50)'; Y = [cos(X), 0.5sin(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π 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π and specify the set of x values for the stem plot.
figure X = linspace(0,2pi,50)'; Y = [cos(X), 0.5sin(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,2pi,50)'; x2 = linspace(pi,3pi,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.
X = linspace(0,10,20)'; Y = (exp(0.25*X)); stem(X,Y,'filled')
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,2pi,25)'; Y = (cos(2X)); stem(X,Y,'LineStyle','-.',... 'MarkerFaceColor','red',... 'MarkerEdgeColor','green')
The stem remains the default color.
Plot Data from a Table
Since R2022b
A convenient way to plot data from a table is to pass the table to the stem
function and specify the variables to plot.
Read the first 100 rows and 7 columns of weather.csv
as a timetable tbl
. Then display the first three rows of the table.
tbl = readtimetable("weather.csv","Range",[1 1 101 7]); head(tbl,3)
Time WindDirection WindSpeed Humidity Temperature RainInchesPerMinute CumulativeRainfall
____________________ _____________ _________ ________ ___________ ___________________ __________________
25-Oct-2021 00:00:09 46 1 84 49.2 0 0
25-Oct-2021 00:01:09 45 1.6 84 49.2 0 0
25-Oct-2021 00:02:09 36 2.2 84 49.2 0 0
Plot the row times on the _x_-axis and the CumulativeRainfall
variable on the _y_-axis. When you plot data from a timetable, the row times are plotted on the _x_-axis by default. Thus, you do not need to specify the Time
variable. Return the Stem
object as h
. Notice that the axis labels match the variable names.
h = stem(tbl,"CumulativeRainfall");
Change the color of the plot to purple by setting the Color
property.
Plot Multiple Table Variables on One Axis
Since R2022b
Create vectors x
, y1
, and y2
, and use them to create a table. Plot the y1
and y2 variables against the x
variable, and use the axis padded
command so that the stems do not overlap with the plot box. Then add a legend, and notice that the legend labels match the table variable names.
x = (0:0.1:2.9)'; y1 = cos(x); y2 = sin(x); tbl = table(x,y1,y2); stem(tbl,"x",["y1","y2"]);
% Pad axes and add a legend axis padded legend
Alternatively, you can omit the x
variable and plot the y1
and y2
variables against the row indices of the table.
stem(tbl,["y1","y2"]); axis padded legend
Specify Axes for Stem Plot
You can display a tiling of plots using the tiledlayout
and nexttile
functions. Call the tiledlayout
function to create a 2-by-1 tiled chart layout. Call the nexttile
function to create the axes objects ax1
and ax2
. Create separate stem plots in the axes by specifying the axes object as the first argument to stem
.
x = 0:25; y1 = exp(0.1x); y2 = -exp(.05x); tiledlayout(2,1)
% Top plot ax1 = nexttile; stem(ax1,x,y1)
% Bottom plot ax2 = nexttile; stem(ax2,x,y2)
Modify Stem Series After Creation
Create a 3-D stem plot and return the stem series object.
X = linspace(0,2); Y = X.^3; Z = exp(X).*cos(Y); h = stem3(X,Y,Z,'filled');
Change the color to magenta and set the marker face color to yellow. Use view
to adjust the angle of the axes in the figure. Use dot notation to set properties.
h.Color = 'm'; h.MarkerFaceColor = 'y'; view(-10,35)
Adjust Baseline Properties
Create a stem plot and change properties of the baseline.
X = linspace(0,2pi,50); Y = exp(0.3X).sin(3X); h = stem(X,Y);
Change the line style of the baseline. Use dot notation to set properties.
hbase = h.BaseLine; hbase.LineStyle = '--';
Hide the baseline by setting its Visible
property to 'off'
.
Change Baseline Level
Create a stem plot with a baseline level at 2.
X = linspace(0,2pi,50)'; Y = (exp(0.3X).sin(3X)); stem(X,Y,'BaseValue',2);
Input Arguments
Y
— Data sequence to display
vector or matrix
Data sequence to display, specified as a vector or matrix. When Y
is a vector, stem
creates one Stem
object. When Y
is a matrix, stem
creates a separate Stem
object for each column.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| categorical
| datetime
| duration
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
| categorical
| datetime
| duration
LineSpec
— Line style, marker, and color
string scalar | character vector
Line style, marker, and color, specified as a string scalar or character vector containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.
Example: "--or"
is a red dashed line with circle markers.
Line Style | Description | Resulting Line |
---|---|---|
"-" | Solid line | ![]() |
"--" | Dashed line | ![]() |
":" | Dotted line | ![]() |
"-." | Dash-dotted line | ![]() |
Marker | Description | Resulting Marker |
---|---|---|
"o" | Circle | ![]() |
"+" | Plus sign | ![]() |
"*" | Asterisk | ![]() |
"." | Point | ![]() |
"x" | Cross | ![]() |
"_" | Horizontal line | ![]() |
"|" | Vertical line | ![]() |
"square" | Square | ![]() |
"diamond" | Diamond | ![]() |
"^" | Upward-pointing triangle | ![]() |
"v" | Downward-pointing triangle | ![]() |
">" | Right-pointing triangle | ![]() |
"<" | Left-pointing triangle | ![]() |
"pentagram" | Pentagram | ![]() |
"hexagram" | Hexagram | ![]() |
Color Name | Short Name | RGB Triplet | Appearance |
---|---|---|---|
"red" | "r" | [1 0 0] | ![]() |
"green" | "g" | [0 1 0] | ![]() |
"blue" | "b" | [0 0 1] | ![]() |
"cyan" | "c" | [0 1 1] | ![]() |
"magenta" | "m" | [1 0 1] | ![]() |
"yellow" | "y" | [1 1 0] | ![]() |
"black" | "k" | [0 0 0] | ![]() |
"white" | "w" | [1 1 1] | ![]() |
tbl
— Source table
table | timetable
Source table containing the data to plot, specified as a table or a timetable.
yvar
— Table variables containing _y_-coordinates
string array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype()
Table variables containing the _y_-coordinates, specified using one of the indexing schemes from the table.
Indexing Scheme | Examples |
---|---|
Variable names: A string, character vector, or cell array.A pattern object. | "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit |
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. | 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable |
Variable type: A vartype subscript that selects variables of a specified type. | vartype("categorical") — All the variables containing categorical values |
The table variables you specify can contain numeric, categorical, datetime, or duration values. If xvar
andyvar
both specify multiple variables, the number of variables must be the same.
Example: stem(tbl,"x",["y1","y2"])
specifies the table variables named y1
and y2
for the_y_-coordinates.
Example: stem(tbl,"x",2)
specifies the second variable for the _y_-coordinates.
Example: stem(tbl,"x",vartype("numeric"))
specifies all numeric variables for the _y_-coordinates.
xvar
— Table variables containing _x_-coordinates
string array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype()
Table variables containing the _x_-coordinates, specified using one of the indexing schemes from the table.
Indexing Scheme | Examples |
---|---|
Variable names: A string, character vector, or cell array.A pattern object. | "A" or 'A' — A variable named A["A","B"] or {'A','B'} — Two variables named A andB"Var"+digitsPattern(1) — Variables named"Var" followed by a single digit |
Variable index: An index number that refers to the location of a variable in the table.A vector of numbers.A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0 or false values. | 3 — The third variable from the table[2 3] — The second and third variables from the table[false false true] — The third variable |
Variable type: A vartype subscript that selects variables of a specified type. | vartype("categorical") — All the variables containing categorical values |
The table variables you specify can contain numeric, categorical, datetime, or duration values. If xvar
andyvar
both specify multiple variables, the number of variables must be the same.
Example: stem(tbl,["x1","x2"],"y")
specifies the table variables named x1
and x2
for the_x_-coordinates.
Example: stem(tbl,2,"y")
specifies the second variable for the _x_-coordinates.
Example: stem(tbl,vartype("numeric"),"y")
specifies all numeric variables for the _x_-coordinates.
ax
— Axes
object
Axes
object
Axes
object. If you do not specify the axes, then stem
plots into the current axes.
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: "LineStyle",":","MarkerFaceColor","red"
plots the stem as a dotted line and colors the marker face red.
The Stem
properties listed here are only a subset. For a complete list, see Stem Properties.
Line style, specified as one of the options listed in this table.
Line Style | Description | Resulting Line |
---|---|---|
"-" | Solid line | ![]() |
"--" | Dashed line | ![]() |
":" | Dotted line | ![]() |
"-." | Dash-dotted line | ![]() |
"none" | No line | No line |
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.
Color
— Stem color
[0 0 0]
(default) | RGB triplet | hexadecimal color code | "r"
| "g"
| "b"
| ...
Stem 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 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]
. - A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (
#
) followed by three or six hexadecimal digits, which can range from0
toF
. The values are not case sensitive. Therefore, 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: "blue"
Example: [0 0 1]
Example: "#0000FF"
Marker
— Marker symbol
"o"
(default) | "+"
| "*"
| "."
| "x"
| ...
Marker symbol, specified as one of the markers listed in this table.
Marker | Description | Resulting Marker |
---|---|---|
"o" | Circle | ![]() |
"+" | Plus sign | ![]() |
"*" | Asterisk | ![]() |
"." | Point | ![]() |
"x" | Cross | ![]() |
"_" | Horizontal line | ![]() |
"|" | Vertical line | ![]() |
"square" | Square | ![]() |
"diamond" | Diamond | ![]() |
"^" | Upward-pointing triangle | ![]() |
"v" | Downward-pointing triangle | ![]() |
">" | Right-pointing triangle | ![]() |
"<" | Left-pointing triangle | ![]() |
"pentagram" | Pentagram | ![]() |
"hexagram" | Hexagram | ![]() |
"none" | No markers | Not applicable |
Example: "+"
Example: "diamond"
Marker size, specified as a positive value in points, where 1 point = 1/72 of an inch.
Marker outline color, specified as "auto"
, an RGB triplet, a hexadecimal color code, a color name, or a short name. The default value of"auto"
uses the same color as the Color property.
For a custom color, specify an RGB triplet or a hexadecimal color code.
- 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]
. - A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (
#
) followed by three or six hexadecimal digits, which can range from0
toF
. The values are not case sensitive. Therefore, 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" | ![]() |
Marker fill color, specified as "auto"
, an RGB triplet, a hexadecimal color code, a color name, or a short name. The "auto"
option uses the same color as the Color property of the parent axes. If you specify"auto"
and the axes plot box is invisible, the marker fill color is the color of the figure.
For a custom color, specify an RGB triplet or a hexadecimal color code.
- 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]
. - A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (
#
) followed by three or six hexadecimal digits, which can range from0
toF
. The values are not case sensitive. Therefore, 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" | ![]() |
Output Arguments
h
— Stem
objects
Stem
objects
Stem
objects. These are unique identifiers, which you can use to modify the properties of a specific Stem
object after it is created.
Extended Capabilities
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The stem
function supports GPU array input with these 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).
Version History
Introduced before R2006a
R2024b: _x_-axis limits are padded by default
Stem plots now have padding on the right and left sides of the_x_-axis to prevent overlap with the edges of the plot box.
R2022b: Pass tables directly to stem
Create plots by passing a table to the stem
function followed by the variables you want to plot. When you specify your data as a table, the axis labels and the legend (if present) are automatically labeled using the table variable names.