plot3 - 3-D line plot - MATLAB (original) (raw)

Syntax

Description

Vector and Matrix Data

plot3([X](#mw%5Fa3f6ce4c-705d-4ffa-a5f8-a1383a37d9ea),[Y](#mw%5F1d59fa62-11af-4422-a926-e21a72c3fbe4),[Z](#mw%5F27398af1-7a0f-479b-bd74-2d29c09e09a9)) plots coordinates in 3-D space.

example

plot3([X](#mw%5Fa3f6ce4c-705d-4ffa-a5f8-a1383a37d9ea),[Y](#mw%5F1d59fa62-11af-4422-a926-e21a72c3fbe4),[Z](#mw%5F27398af1-7a0f-479b-bd74-2d29c09e09a9),[LineSpec](#f30-448776%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)) creates the plot using the specified line style, marker, and color.

example

plot3([X](#mw%5Fa3f6ce4c-705d-4ffa-a5f8-a1383a37d9ea)1,[Y](#mw%5F1d59fa62-11af-4422-a926-e21a72c3fbe4)1,[Z](#mw%5F27398af1-7a0f-479b-bd74-2d29c09e09a9)1,...,[X](#mw%5Fa3f6ce4c-705d-4ffa-a5f8-a1383a37d9ea)n,[Y](#mw%5F1d59fa62-11af-4422-a926-e21a72c3fbe4)n,[Z](#mw%5F27398af1-7a0f-479b-bd74-2d29c09e09a9)n) plots multiple sets of coordinates on the same set of axes. Use this syntax as an alternative to specifying multiple sets as matrices.

example

plot3([X](#mw%5Fa3f6ce4c-705d-4ffa-a5f8-a1383a37d9ea)1,[Y](#mw%5F1d59fa62-11af-4422-a926-e21a72c3fbe4)1,[Z](#mw%5F27398af1-7a0f-479b-bd74-2d29c09e09a9)1,[LineSpec](#f30-448776%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)1,...,[X](#mw%5Fa3f6ce4c-705d-4ffa-a5f8-a1383a37d9ea)n,[Y](#mw%5F1d59fa62-11af-4422-a926-e21a72c3fbe4)n,[Z](#mw%5F27398af1-7a0f-479b-bd74-2d29c09e09a9)n,[LineSpec](#f30-448776%5Fsep%5Fmw%5F3a76f056-2882-44d7-8e73-c695c0c54ca8)n) assigns specific line styles, markers, and colors to each XYZ triplet. You can specify LineSpec for some triplets and omit it for others. For example, plot3(X1,Y1,Z1,'o',X2,Y2,Z2) specifies markers for the first triplet but not for the second triplet.

example

Table Data

plot3([tbl](#f30-448776%5Fsep%5Fmw%5F1f5b8358-45d8-4c4d-8a0d-17e2da07c841),[xvar](#mw%5F3bc8cfc7-8772-485b-8a6a-fd0b3cf289e9),[yvar](#mw%5F0da04d76-d633-4e7a-af83-f64484832286),[zvar](#mw%5Fb8029990-d54c-4d35-83ee-a82ebce02810)) plots the variables xvar, yvar, andzvar from the table tbl. To plot one data set, specify one variable each for xvar, yvar, andzvar. To plot multiple data sets, specify multiple variables for at least one of those arguments. The arguments that specify multiple variables must specify the same number of variables. (since R2022a)

example

Additional Options

plot3([ax](#mw%5Fd8d232f8-cc75-4a69-aff3-4525d0cce4f2),___) displays the plot in the target axes. Specify the axes as the first argument in any of the previous syntaxes.

example

plot3(___,[Name,Value](#namevaluepairarguments)) specifiesLine properties using one or more name-value pair arguments. Specify the properties after all other input arguments. For a list of properties, see Line Properties.

example

`p` = plot3(___) returns aLine object or an array of Line objects. Usep to modify properties of the plot after creating it. For a list of properties, see Line Properties.

example

Examples

collapse all

Define t as a vector of values between 0 and 10π. Define st and ct as vectors of sine and cosine values. Then plot st, ct, and t.

t = 0:pi/50:10*pi; st = sin(t); ct = cos(t); plot3(st,ct,t)

Figure contains an axes object. The axes object contains an object of type line.

Create two sets of _x_-, _y_-, and _z_-coordinates.

t = 0:pi/500:pi; xt1 = sin(t).cos(10t); yt1 = sin(t).sin(10t); zt1 = cos(t);

xt2 = sin(t).cos(12t); yt2 = sin(t).sin(12t); zt2 = cos(t);

Call the plot3 function, and specify consecutive XYZ triplets.

plot3(xt1,yt1,zt1,xt2,yt2,zt2)

Figure contains an axes object. The axes object contains 2 objects of type line.

Create matrix X containing three rows of _x_-coordinates. Create matrix Y containing three rows of _y_-coordinates.

t = 0:pi/500:pi; X(1,:) = sin(t).cos(10t); X(2,:) = sin(t).cos(12t); X(3,:) = sin(t).cos(20t);

Y(1,:) = sin(t).sin(10t); Y(2,:) = sin(t).sin(12t); Y(3,:) = sin(t).sin(20t);

Create matrix Z containing the _z_-coordinates for all three sets.

Plot all three sets of coordinates on the same set of axes.

Figure contains an axes object. The axes object contains 3 objects of type line.

Create vectors xt, yt, and zt.

t = 0:pi/500:40*pi; xt = (3 + cos(sqrt(32)*t)).*cos(t); yt = sin(sqrt(32) * t); zt = (3 + cos(sqrt(32)*t)).*sin(t);

Plot the data, and use the axis equal command to space the tick units equally along each axis. Then specify the labels for each axis.

plot3(xt,yt,zt) axis equal xlabel('x(t)') ylabel('y(t)') zlabel('z(t)')

Figure contains an axes object. The axes object with xlabel x(t), ylabel y(t) contains an object of type line.

Create vectors t, xt, and yt, and plot the points in those vectors using circular markers.

t = 0:pi/20:10*pi; xt = sin(t); yt = cos(t); plot3(xt,yt,t,'o')

Figure contains an axes object. The axes contains a line object which displays its values using only markers.

Create vectors t, xt, and yt, and plot the points in those vectors as a blue line with 10-point circular markers. Use a hexadecimal color code to specify a light blue fill color for the markers.

t = 0:pi/20:10*pi; xt = sin(t); yt = cos(t); plot3(xt,yt,t,'-o','Color','b','MarkerSize',10,... 'MarkerFaceColor','#D9FFFF')

Figure contains an axes object. The axes object contains an object of type line.

Create vector t. Then use t to calculate two sets of x and y values.

t = 0:pi/20:10*pi; xt1 = sin(t); yt1 = cos(t);

xt2 = sin(2t); yt2 = cos(2t);

Plot the two sets of values. Use the default line for the first set, and specify a dashed line for the second set.

plot3(xt1,yt1,t,xt2,yt2,t,'--')

Figure contains an axes object. The axes object contains 2 objects of type line.

Create vectors t, xt, and yt, and plot the data in those vectors. Return the chart line in the output variable p.

t = linspace(-10,10,1000); xt = exp(-t./10).sin(5t); yt = exp(-t./10).cos(5t); p = plot3(xt,yt,t);

Figure contains an axes object. The axes object contains an object of type line.

Change the line width to 3.

Figure contains an axes object. The axes object contains an object of type line.

Since R2022a

A convenient way to plot data from a table is to pass the table to the plot3 function and specify the variables to plot.

Create vectors x, y, and t, and put the vectors in a table. Then display the first three rows of the table.

t = (0:pi/20:10*pi)'; x = sin(t); y = cos(t); tbl = table(x,y,t); head(tbl,3)

   x          y          t   
_______    _______    _______

      0          1          0
0.15643    0.98769    0.15708
0.30902    0.95106    0.31416

Plot the x, y, and t table variables. Return the Line object as p. Notice that the axis labels match the variable names.

p = plot3(tbl,"x","y","t");

Figure contains an axes object. The axes object with xlabel x, ylabel y contains an object of type line.

To modify aspects of the line, set the LineStyle, Color, and Marker properties on the Line object. For example, change the line to a red dotted line with circular markers.

p.LineStyle = ":"; p.Color = "red"; p.Marker = "o";

Figure contains an axes object. The axes object with xlabel x, ylabel y contains an object of type line.

Since R2022a

Create a table containing five variables. Then display the first three rows of the table.

t = (0:pi/500:pi)'; x1 = sin(t).cos(10t); x2 = sin(t).cos(12t); y1 = sin(t).sin(10t); y2 = sin(t).sin(12t); z = cos(t); tbl = table(x1,x2,y1,y2,z); head(tbl,3)

   x1           x2            y1            y2           z   
_________    _________    __________    __________    _______

        0            0             0             0          1
0.0062707    0.0062653    0.00039452    0.00047329    0.99998
 0.012467     0.012423     0.0015749     0.0018877    0.99992

Plot the x1 and x2 variables on the _x_-axis, the y1 and y2 variables on the _y_-axis, and the z variable on the _z_-axis. Then add a legend. Notice that the legend entries match the variable names.

plot3(tbl,["x1","x2"],["y1","y2"],"z") legend

Figure contains an axes object. The axes object contains 2 objects of type line.

You can display a tiling of plots using the tiledlayout and nexttile functions. Call the tiledlayout function to create a 1-by-2 tiled chart layout. Call the nexttile function to create the axes objects ax1 and ax2. Create separate line plots in the axes by specifying the axes object as the first argument to plot3.

tiledlayout(1,2)

% Left plot ax1 = nexttile; t = 0:pi/20:10*pi; xt1 = sin(t); yt1 = cos(t); plot3(ax1,xt1,yt1,t) title(ax1,'Helix With 5 Turns')

% Right plot ax2 = nexttile; t = 0:pi/20:10pi; xt2 = sin(2t); yt2 = cos(2*t); plot3(ax2,xt2,yt2,t) title(ax2,'Helix With 10 Turns')

Figure contains 2 axes objects. Axes object 1 with title Helix With 5 Turns contains an object of type line. Axes object 2 with title Helix With 10 Turns contains an object of type line.

Create x and y as vectors of random values between 0 and 1. Create z as a vector of random duration values.

x = rand(1,10); y = rand(1,10); z = duration(rand(10,1),randi(60,10,1),randi(60,10,1));

Plot x, y, and z, and specify the format for the _z_-axis as minutes and seconds. Then add axis labels, and turn on the grid to make it easier to visualize the points within the plot box.

plot3(x,y,z,'o','DurationTickFormat','mm:ss') xlabel('X') ylabel('Y') zlabel('Duration') grid on

Figure contains an axes object. The axes object with xlabel X, ylabel Y contains a line object which displays its values using only markers.

Create vectors xt, yt, and zt. Plot the values, specifying a solid line with circular markers using the LineSpec argument. Specify the MarkerIndices property to place one marker at the 200th data point.

t = 0:pi/500:pi; xt(1,:) = sin(t).cos(10t); yt(1,:) = sin(t).sin(10t); zt = cos(t); plot3(xt,yt,zt,'-o','MarkerIndices',200)

Figure contains an axes object. The axes object contains an object of type line.

Input Arguments

collapse all

_x_-coordinates, specified as a scalar, vector, or matrix. The size and shape of X depends on the shape of your data and the type of plot you want to create. This table describes the most common situations.

Type of Plot How to Specify Coordinates
Single point Specify X, Y, andZ as scalars and include a marker. For example:plot3(1,2,3,'o')
One set of points Specify X, Y, andZ as any combination of row or column vectors of the same length. For example:plot3([1 2 3],[4; 5; 6],[7 8 9])
Multiple sets of points (using vectors) Specify consecutive sets of X,Y, and Z vectors. For example:plot3([1 2 3],[4 5 6],[7 8 9],[1 2 3],[4 5 6],[10 11 12])
Multiple sets of points(using matrices) Specify at least one of X, Y, or Z as a matrix, and the others as vectors. Each ofX, Y, and Z must have at least one dimension that is same size. For best results, specify all vectors of the same shape and all matrices of the same shape. For example:plot3([1 2 3],[4 5 6],[7 8 9; 10 11 12])

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

_y_-coordinates, specified as a scalar, vector, or matrix. The size and shape of Y depends on the shape of your data and the type of plot you want to create. This table describes the most common situations.

Type of Plot How to Specify Coordinates
Single point Specify X, Y, andZ as scalars and include a marker. For example:plot3(1,2,3,'o')
One set of points Specify X, Y, andZ as any combination of row or column vectors of the same length. For example:plot3([1 2 3],[4; 5; 6],[7 8 9])
Multiple sets of points (using vectors) Specify consecutive sets of X,Y, and Z vectors. For example:plot3([1 2 3],[4 5 6],[7 8 9],[1 2 3],[4 5 6],[10 11 12])
Multiple sets of points(using matrices) Specify at least one of X, Y, or Z as a matrix, and the others as vectors. Each ofX, Y, and Z must have at least one dimension that is same size. For best results, specify all vectors of the same shape and all matrices of the same shape. For example:plot3([1 2 3],[4 5 6],[7 8 9; 10 11 12])

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

_z_-coordinates, specified as a scalar, vector, or matrix. The size and shape of Z depends on the shape of your data and the type of plot you want to create. This table describes the most common situations.

Type of Plot How to Specify Coordinates
Single point Specify X, Y, andZ as scalars and include a marker. For example:plot3(1,2,3,'o')
One set of points Specify X, Y, andZ as any combination of row or column vectors of the same length. For example:plot3([1 2 3],[4; 5; 6],[7 8 9])
Multiple sets of points (using vectors) Specify consecutive sets of X,Y, and Z vectors. For example:plot3([1 2 3],[4 5 6],[7 8 9],[1 2 3],[4 5 6],[10 11 12])
Multiple sets of points(using matrices) Specify at least one of X, Y, or Z as a matrix, and the others as vectors. Each ofX, Y, and Z must have at least one dimension that is same size. For best results, specify all vectors of the same shape and all matrices of the same shape. For example:plot3([1 2 3],[4 5 6],[7 8 9; 10 11 12])

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

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 Sample of solid line
"--" Dashed line Sample of dashed line
":" Dotted line Sample of dotted line
"-." Dash-dotted line Sample of dash-dotted line, with alternating dashes and dots
Marker Description Resulting Marker
"o" Circle Sample of circle marker
"+" Plus sign Sample of plus sign marker
"*" Asterisk Sample of asterisk marker
"." Point Sample of point marker
"x" Cross Sample of cross marker
"_" Horizontal line Sample of horizontal line marker
"|" Vertical line Sample of vertical line marker
"square" Square Sample of square marker
"diamond" Diamond Sample of diamond marker
"^" Upward-pointing triangle Sample of upward-pointing triangle marker
"v" Downward-pointing triangle Sample of downward-pointing triangle marker
">" Right-pointing triangle Sample of right-pointing triangle marker
"<" Left-pointing triangle Sample of left-pointing triangle marker
"pentagram" Pentagram Sample of pentagram marker
"hexagram" Hexagram Sample of hexagram marker
Color Name Short Name RGB Triplet Appearance
"red" "r" [1 0 0] Sample of the color red
"green" "g" [0 1 0] Sample of the color green
"blue" "b" [0 0 1] Sample of the color blue
"cyan" "c" [0 1 1] Sample of the color cyan
"magenta" "m" [1 0 1] Sample of the color magenta
"yellow" "y" [1 1 0] Sample of the color yellow
"black" "k" [0 0 0] Sample of the color black
"white" "w" [1 1 1] Sample of the color white

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

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 you specify multiple variables for more than one argument, the number of variables must be the same for each of those arguments.

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

Example: plot3(tbl,2,"y","z") specifies the second variable for the _x_-coordinates.

Example: plot3(tbl,vartype("numeric"),"y","z") specifies all numeric variables for the _x_-coordinates.

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 you specify multiple variables for more than one argument, the number of variables must be the same for each of those arguments.

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

Example: plot3(tbl,"x",2,"z") specifies the second variable for the _y_-coordinates.

Example: plot3(tbl,"x",vartype("numeric"),"z") specifies all numeric variables for the _y_-coordinates.

Table variables containing the _z_-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 you specify multiple variables for more than one argument, the number of variables must be the same for each of those arguments.

Example: plot3(tbl,"x","y",["z1","z2"]) specifies the table variables named z1 and z2 for the_z_-coordinates.

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

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

Target axes, specified as an Axes object. If you do not specify the axes and if the current axes is Cartesian, then plot3 uses the current axes.

Name-Value Arguments

collapse all

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: plot3([1 2],[3 4],[5 6],'Color','red') specifies a red line for the plot.

Note

The properties listed here are only a subset. For a complete list, see Line Properties.

Color, specified as an RGB triplet, a hexadecimal color code, a color name, or a short name. The color you specify sets the line color. It also sets the marker edge color when the MarkerEdgeColor property is set to'auto'.

For a custom color, specify an RGB triplet or a hexadecimal color code.

Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and the hexadecimal color codes.

Color Name Short Name RGB Triplet Hexadecimal Color Code Appearance
"red" "r" [1 0 0] "#FF0000" Sample of the color red
"green" "g" [0 1 0] "#00FF00" Sample of the color green
"blue" "b" [0 0 1] "#0000FF" Sample of the color blue
"cyan" "c" [0 1 1] "#00FFFF" Sample of the color cyan
"magenta" "m" [1 0 1] "#FF00FF" Sample of the color magenta
"yellow" "y" [1 1 0] "#FFFF00" Sample of the color yellow
"black" "k" [0 0 0] "#000000" Sample of the color black
"white" "w" [1 1 1] "#FFFFFF" Sample of the color white
"none" Not applicable Not applicable Not applicable No color

This table lists the default color palettes for plots in the light and dark themes.

Palette Palette Colors
"gem" — Light theme default_Before R2025a: Most plots use these colors by default._ Sample of the "gem" color palette
"glow" — Dark theme default Sample of the "glow" color palette

You can get the RGB triplets and hexadecimal color codes for these palettes using the orderedcolors and rgb2hex functions. For example, get the RGB triplets for the "gem" palette and convert them to hexadecimal color codes.

RGB = orderedcolors("gem"); H = rgb2hex(RGB);

Before R2023b: Get the RGB triplets using RGB = get(groot,"FactoryAxesColorOrder").

Before R2024a: Get the hexadecimal color codes using H = compose("#%02X%02X%02X",round(RGB*255)).

Marker size, specified as a positive value in points, where 1 point = 1/72 of an inch.

Tips

Extended Capabilities

expand all

The plot3 function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

expand all

When you pass a table and one or more variable names to the plot3 function, the axis and legend labels now display any special characters that are included in the table variable names, such as underscores. Previously, special characters were interpreted as TeX or LaTeX characters.

For example, if you pass a table containing a variable named Sample_Number to the plot3 function, the underscore appears in the axis and legend labels. In R2022a and earlier releases, the underscores are interpreted as subscripts.

Release Label for Table Variable "Sample_Number"
R2022b Label for the variable name "Sample_Number" in R2022b. The underscore is included in the label.
R2022a Label for the variable name "Sample_Number" in R2022a. The N character displays as a subscript.

To display axis and legend labels with TeX or LaTeX formatting, specify the labels manually. For example, after plotting, call the xlabel orlegend function with the desired label strings.

xlabel("Sample_Number") legend(["Sample_Number" "Another_Legend_Label"])

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