stairs - Stairstep graph - MATLAB (2024)

Table of Contents
Syntax Description Vector and Matrix Data Table Data Additional Options Examples Plot Single Data Series Plot Multiple Data Series Plot Single Data Series at Specified x-Values Plot Multiple Data Series at Specified x-Values Plot Multiple Data Series at Unique Sets of x-Values Specify Line Style, Marker Symbol and Color Specify Additional Style Options Plot Data from a Table Plot Multiple Table Variables on One Axes Specify Axes for Stairstep Plots Modify Stairstep Plot After Creation Create a Stairstep Plot using plot Function Input Arguments Y — y values vector or matrix X — x values vector or matrix LineSpec — Line style, marker, and color string scalar | character vector tbl — Source table table | timetable yvar — Table variables containing y-coordinatesstring array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype() xvar — Table variables containing x-coordinates string array | character vector | cell array | pattern | numeric scalar or vector | logical vector | vartype() ax — Axes object Axes object Name-Value Arguments Color — Line color [0 0.4470 0.7410] (default) | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ... Output Arguments h — Stair objects Stair objects xb — x values for use with plot vector or matrix yb — y values for use with plot vector or matrix Extended Capabilities GPU Arrays Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™. Distributed Arrays Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™. Version History R2022b: Pass tables directly to stairs See Also Functions Properties Topics MATLAB Command Americas Europe Asia Pacific

Stairstep graph

collapse all in page

  • stairs - Stairstep graph - MATLAB (1)

Syntax

stairs(Y)

stairs(X,Y)

stairs(___,LineSpec)

stairs(tbl,yvar)

stairs(tbl,xvar,yvar)

stairs(___,Name,Value)

stairs(ax,___)

h = stairs(___)

[xb,yb]= stairs(___)

Description

Vector and Matrix Data

stairs(Y) draws a stairstep graph of the elements in Y.

  • If Y is a vector, then stairs draws one line.

  • If Y is a matrix, then stairs draws one line per matrix column.

example

stairs(X,Y) plots the elements in Y at the locations specified by X. The inputs X and Y 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.

example

stairs(___,LineSpec) specifies a line style, marker symbol, and color. For example, ":*r" specifies a dotted red line with asterisk markers. Use this option with any of the input argument combinations in the previous syntaxes.

example

Table Data

stairs(tbl,yvar) 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 for yvar. (since R2022b)

example

stairs(tbl,xvar,yvar) plots the variables xvar and yvar from the table tbl. You can specify one or multiple variables for xvar and yvar. If both arguments specify multiple variables, they must specify the same number of variables. (since R2022b)

example

Additional Options

stairs(___,Name,Value) modifies the stairstep chart using one or more name-value pair arguments. For example, "Marker","o","MarkerSize",8 specifies 8 point circle markers.

example

stairs(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.

example

h = stairs(___) returns one or more Stair objects. Use h to make changes to properties of a specific Stair object after it is created.

example

[xb,yb]= stairs(___) does not create a plot, but returns matrices xb and yb of the same size, such that plot(xb,yb) plots the stairstep graph.

This syntax does not support the table and table variable arguments.

example

Examples

collapse all

Plot Single Data Series

Open Live Script

Create a stairstep plot of sine evaluated at 40 equally spaced values between 0 and 4π.

X = linspace(0,4*pi,40);Y = sin(X);figurestairs(Y)

stairs - Stairstep graph - MATLAB (2)

The length of Y automatically determines and generates the x-axis scale.

Plot Multiple Data Series

Open Live Script

Create a stairstep plot of two cosine functions evaluated at 50 equally spaced values between 0 and 4π.

X = linspace(0,4*pi,50)';Y = [0.5*cos(X), 2*cos(X)];figurestairs(Y)

stairs - Stairstep graph - MATLAB (3)

The number of rows in Y automatically determines and generates the x-axis scale.

Plot Single Data Series at Specified x-Values

Open Live Script

Create a stairstep plot of a sine wave evaluated at equally spaced values between 0 and 4π. Specify the set of x-values for the plot.

X = linspace(0,4*pi,40);Y = sin(X);figurestairs(X,Y)

The entries in Y are plotted against the corresponding entries in X.

Plot Multiple Data Series at Specified x-Values

Open Live Script

Create a stairstep plot of two cosine waves evaluated at equally spaced values between 0 and 4π. Specify the set of x-values for the plot.

X = linspace(0,4*pi,50)';Y = [0.5*cos(X), 2*cos(X)];figurestairs(X,Y)

stairs - Stairstep graph - MATLAB (5)

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

Plot Multiple Data Series at Unique Sets of x-Values

Open Live Script

Create a stairstep plot of two sine waves evaluated at different values. Specify a unique set of x-values for plotting each data series.

x1 = linspace(0,2*pi)';x2 = linspace(0,pi)';X = [x1,x2];Y = [sin(5*x1),exp(x2).*sin(5*x2)];figurestairs(X,Y)

stairs - Stairstep graph - MATLAB (6)

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

Specify Line Style, Marker Symbol and Color

Open Live Script

Create a stairstep plot and set the line style to a dot-dashed line, the marker symbol to circles, and the color to red.

X = linspace(0,4*pi,20);Y = sin(X);figurestairs(Y, '-.or')

stairs - Stairstep graph - MATLAB (7)

Specify Additional Style Options

Open Live Script

Create a stairstep plot and set the line width to 2, the marker symbols to diamonds, and the marker face color to cyan using Name,Value pair arguments.

X = linspace(0,4*pi,20);Y = sin(X);figurestairs(Y,'LineWidth',2,'Marker','d','MarkerFaceColor','c')

stairs - Stairstep graph - MATLAB (8)

Plot Data from a Table

Since R2022b

Open Live Script

A convenient way to plot data from a table is to pass the table to the stairs 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 Time variable on the x-axis and the CumulativeRainfall variable on the y-axis. Then use the axis padded command so that the line and the plot box do not overlap.

Return the Stair object as h. Notice that the axis labels match the variable names.

h = stairs(tbl,"Time","CumulativeRainfall");axis padded

stairs - Stairstep graph - MATLAB (9)

Change the color of the line to purple by setting the Color property.

h.Color = [0.5 0 0.8];

stairs - Stairstep graph - MATLAB (10)

Plot Multiple Table Variables on One Axes

Since R2022b

Open Live Script

Create vectors x, y1, and y2, and use them to create a table. Plot the y1 and y2 variables against the x variable. Use the axis padded command so that the line and the plot box do not overlap.

Add a legend, and notice that the legend labels match the variable names.

x = linspace(0,6,20);y1 = cos(x);y2 = sin(x);tbl = table(x,y1,y2);stairs(tbl,"x",["y1","y2"]);% Pad x- and y-axes, and add legendaxis paddedlegend

stairs - Stairstep graph - MATLAB (11)

Alternatively, you can omit the x variable and plot the y1 and y2 variables against the row indices of the table.

stairs(tbl,["y1","y2"]);axis paddedlegend

stairs - Stairstep graph - MATLAB (12)

Specify Axes for Stairstep Plots

Open Live Script

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 stairstep plots in the axes by specifying the axes object as the first argument to stairs.

x = linspace(0,2*pi);y1 = 5*sin(x);y2 = sin(5*x);tiledlayout(2,1)% Top plotax1 = nexttile;stairs(ax1,x,y1)% Bottom plotax2 = nexttile; stairs(ax2,x,y2)

stairs - Stairstep graph - MATLAB (13)

Modify Stairstep Plot After Creation

Open Live Script

Create a stairstep plot of two data series and return the two stair objects.

X = linspace(0,1,30)';Y = [cos(10*X), exp(X).*sin(10*X)];h = stairs(X,Y);

stairs - Stairstep graph - MATLAB (14)

Use small circle markers for the first data series. Use magenta filled circles for the second series. Use dot notation to set properties.

h(1).Marker = 'o';h(1).MarkerSize = 4;h(2).Marker = 'o';h(2).MarkerFaceColor = 'm';

stairs - Stairstep graph - MATLAB (15)

Create a Stairstep Plot using plot Function

Open Live Script

Evaluate two cosine functions at 50 equally spaced values between 0 and 4π and create a stairstep plot using plot.

X = linspace(0,4*pi,50)';Y = [0.5*cos(X), 2*cos(X)];[xb,yb] = stairs(X,Y);

stairs returns two matrices of the same size, xb and yb, but no plot.

Use plot to create the stairstep plot with xb and yb.

figureplot(xb,yb)

stairs - Stairstep graph - MATLAB (16)

Input Arguments

collapse all

Yy values
vector or matrix

y values, specified as a vector or matrix.When Y is a vector, stairs createsone stair object. When Y is a matrix, stairs drawsone line per matrix column and creates a separate stair object foreach column.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

Xx values
vector or matrix

x values, specified as a vector or matrix.When Y is a vector, X must bea vector of the same size. When Y is a matrix, X mustbe a matrix of the same size, or a vector whose length equals thenumber of rows in Y.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | categorical | datetime | duration

LineSpecLine 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 StyleDescriptionResulting Line
"-"Solid line

stairs - Stairstep graph - MATLAB (17)

"--"Dashed line

stairs - Stairstep graph - MATLAB (18)

":"Dotted line

stairs - Stairstep graph - MATLAB (19)

"-."Dash-dotted line

stairs - Stairstep graph - MATLAB (20)

MarkerDescriptionResulting Marker
"o"Circle

stairs - Stairstep graph - MATLAB (21)

"+"Plus sign

stairs - Stairstep graph - MATLAB (22)

"*"Asterisk

stairs - Stairstep graph - MATLAB (23)

"."Point

stairs - Stairstep graph - MATLAB (24)

"x"Cross

stairs - Stairstep graph - MATLAB (25)

"_"Horizontal line

stairs - Stairstep graph - MATLAB (26)

"|"Vertical line

stairs - Stairstep graph - MATLAB (27)

"square"Square

stairs - Stairstep graph - MATLAB (28)

"diamond"Diamond

stairs - Stairstep graph - MATLAB (29)

"^"Upward-pointing triangle

stairs - Stairstep graph - MATLAB (30)

"v"Downward-pointing triangle

stairs - Stairstep graph - MATLAB (31)

">"Right-pointing triangle

stairs - Stairstep graph - MATLAB (32)

"<"Left-pointing triangle

stairs - Stairstep graph - MATLAB (33)

"pentagram"Pentagram

stairs - Stairstep graph - MATLAB (34)

"hexagram"Hexagram

stairs - Stairstep graph - MATLAB (35)

Color NameShort NameRGB TripletAppearance
"red""r"[1 0 0]

stairs - Stairstep graph - MATLAB (36)

"green""g"[0 1 0]

stairs - Stairstep graph - MATLAB (37)

"blue""b"[0 0 1]

stairs - Stairstep graph - MATLAB (38)

"cyan" "c"[0 1 1]

stairs - Stairstep graph - MATLAB (39)

"magenta""m"[1 0 1]

stairs - Stairstep graph - MATLAB (40)

"yellow""y"[1 1 0]

stairs - Stairstep graph - MATLAB (41)

"black""k"[0 0 0]

stairs - Stairstep graph - MATLAB (42)

"white""w"[1 1 1]

stairs - Stairstep graph - MATLAB (43)

tblSource table
table | timetable

Source table containing the data to plot, specified as a table or a timetable.

yvarTable 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 SchemeExamples

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 and B

  • "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 trailing 0 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 and yvar both specify multiple variables, the number of variables must be the same.

Example: stairs(tbl,"x",["y1","y2"]) specifies the table variables named y1 and y2 for the y-coordinates.

Example: stairs(tbl,"x",2) specifies the second variable for the y-coordinates.

Example: stairs(tbl,"x",vartype("numeric")) specifies all numeric variables for the y-coordinates.

xvarTable 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 SchemeExamples

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 and B

  • "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 trailing 0 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 and yvar both specify multiple variables, the number of variables must be the same.

Example: stairs(tbl,["x1","x2"],"y") specifies the table variables named x1 and x2 for the x-coordinates.

Example: stairs(tbl,2,"y") specifies the second variable for the x-coordinates.

Example: stairs(tbl,vartype("numeric"),"y") specifies all numeric variables for the x-coordinates.

axAxes object
Axes object

Axes object. If you do not specify the axes,then stairs plots into the current axes.

Name-Value Arguments

Specify optional pairs of arguments as Name1=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: "Marker","s","MarkerFaceColor","red" plots the stairstep graph with red square markers.

The properties listed here are only a subset. For a completelist, see Stair Properties.

ColorLine 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 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 from 0 to F. 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 NameShort NameRGB TripletHexadecimal Color CodeAppearance
"red""r"[1 0 0]"#FF0000"

stairs - Stairstep graph - MATLAB (44)

"green""g"[0 1 0]"#00FF00"

stairs - Stairstep graph - MATLAB (45)

"blue""b"[0 0 1]"#0000FF"

stairs - Stairstep graph - MATLAB (46)

"cyan" "c"[0 1 1]"#00FFFF"

stairs - Stairstep graph - MATLAB (47)

"magenta""m"[1 0 1]"#FF00FF"

stairs - Stairstep graph - MATLAB (48)

"yellow""y"[1 1 0]"#FFFF00"

stairs - Stairstep graph - MATLAB (49)

"black""k"[0 0 0]"#000000"

stairs - Stairstep graph - MATLAB (50)

"white""w"[1 1 1]"#FFFFFF"

stairs - Stairstep graph - MATLAB (51)

"none"Not applicableNot applicableNot applicableNo color

Here are the RGB triplets and hexadecimal color codes for the default colors MATLAB® uses in many types of plots.

RGB TripletHexadecimal Color CodeAppearance
[0 0.4470 0.7410]"#0072BD"

stairs - Stairstep graph - MATLAB (52)

[0.8500 0.3250 0.0980]"#D95319"

stairs - Stairstep graph - MATLAB (53)

[0.9290 0.6940 0.1250]"#EDB120"

stairs - Stairstep graph - MATLAB (54)

[0.4940 0.1840 0.5560]"#7E2F8E"

stairs - Stairstep graph - MATLAB (55)

[0.4660 0.6740 0.1880]"#77AC30"

stairs - Stairstep graph - MATLAB (56)

[0.3010 0.7450 0.9330]"#4DBEEE"

stairs - Stairstep graph - MATLAB (57)

[0.6350 0.0780 0.1840]"#A2142F"

stairs - Stairstep graph - MATLAB (58)

Example: "blue"

Example: [00 1]

Example: "#0000FF"

Output Arguments

collapse all

hStair objects
Stair objects

Stair objects. These are unique identifiers,which you can use to query and modify the properties of a specific Stair objectafter it is created.

xb — x values for use with plot
vector or matrix

x values for use with plot,returned as a vector or matrix. xb contains theappropriate values such that plot(xb,yb) createsthe stairstep graph.

yb — y values for use with plot
vector or matrix

y values for use with plot,returned as a vector or matrix. yb contains theappropriate values such that plot(xb,yb) createsthe stairstep graph.

Extended Capabilities

Version History

Introduced before R2006a

expand all

Create plots by passing a table to the stairs 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.

See Also

Functions

  • bar | histogram | stem

Properties

  • Stair Properties

Topics

  • Plot Dates and Times
  • Plot Categorical Data
  • Plots That Support Tables

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.

stairs - Stairstep graph - MATLAB (59)

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:

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
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

stairs - Stairstep graph - MATLAB (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Corie Satterfield

Last Updated:

Views: 6800

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.