You are on page 1of 31

29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

scatter3
3-D scatter plot

Syntax

scatter3(X,Y,Z)
scatter3(X,Y,Z,S)
scatter3(X,Y,Z,S,C)
scatter3( ___ ,'filled')
scatter3( ___ ,markertype)

scatter3(tbl,xvar,yvar,zvar)
scatter3(tbl,xvar,yvar,zvar,'filled')

scatter3(ax, ___ )
scatter3( ___ ,Name,Value)
h = scatter3( ___ )

Description

Vector and Matrix Data


scatter3(X,Y,Z) displays circles at the locations specified by X, Y, and Z. example

To plot one set of coordinates, specify X, Y, and Z as vectors of equal length.

To plot multiple sets of coordinates on the same set of axes, specify at least one of X, Y, or Z as a
matrix. (Since R2022a)

example
scatter3(X,Y,Z,S) specifies the circle sizes.

To vary the circle sizes, specify S as a vector.

To specify different sizes across multiple sets of coordinates, specify a matrix. (Since R2022a)

example
scatter3(X,Y,Z,S,C) specifies the circle colors. You can specify one color for all the circles, or you can
vary the color. For example, you can plot all red circles by specifying C as "red".

example
scatter3( ___ ,'filled') fills in the circles, using any of the input argument combinations in the
previous syntaxes.

example
scatter3( ___ ,markertype) specifies the marker type.

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 1/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Table Data
scatter3(tbl,xvar,yvar,zvar) plots the variables xvar, yvar, and zvar from the table tbl. To plot example
one data set, specify one variable each for xvar, yvar, and zvar. 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 R2021b)

example
scatter3(tbl,xvar,yvar,zvar,'filled') plots the specified variables from the table with filled
circles. (Since R2021b)

Additional Options
scatter3(ax, ___ ) plots into the axes specified by ax instead of into the current axes (gca). The ax example
option can precede any of the input argument combinations in the previous syntaxes.

example
scatter3( ___ ,Name,Value) modifies the scatter plot using one or more name-value arguments to set
properties. For example:

scatter3(x,y,z,'LineWidth',2) creates a scatter plot with 2-point marker outlines.

scatter3(tbl,'MyX','MyY','MyZ','ColorVariable','MyColors') creates a scatter plot from


data in a table, and customizes the marker colors using data from the table.

For a full list of properties, see Scatter Properties.

example
h = scatter3( ___ ) returns the Scatter object. Use h to modify properties of the scatter chart after it is
created.
Examples collapse all

 Create 3-D Scatter Plot

Create a 3-D scatter plot. Use sphere to define vectors x, y, and z.


Try This Example

 Copy Command

figure  Get
[X,Y,Z] = sphere(16);
x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];
scatter3(x,y,z)

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 2/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

 Vary Marker Size

Use sphere to define vectors x, y, and z.


Try This Example

 Copy Command

[X,Y,Z] = sphere(16);  Get


x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];

Define vector s to specify the marker sizes.

S = repmat([100,50,5],numel(X),1);  Get
s = S(:);

Create a 3-D scatter plot and use view to change the angle of the axes in the figure.

figure  Get
scatter3(x,y,z,s)
view(40,35)

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 3/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Corresponding entries in x, y, z, and s determine the location and size of each marker.

 Vary Marker Color

Use sphere to define vectors x, y, and z.


Try This Example

 Copy Command

[X,Y,Z] = sphere(16);  Get


x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];

Define vectors s and c to specify the size and color of each marker.

S = repmat([50,25,10],numel(X),1);  Get
C = repmat([1,2,3],numel(X),1);
s = S(:);
c = C(:);

Create a 3-D scatter plot and use view to change the angle of the axes in the figure.

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 4/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

figure  Get
scatter3(x,y,z,s,c)
view(40,35)

Corresponding entries in x, y, z, and c determine the location and color of each marker.

 Fill in Markers

Create vectors x and y as cosine and sine values with random noise.
Try This Example

 Copy Command

z = linspace(0,4*pi,250);  Get
x = 2*cos(z) + rand(1,250);
y = 2*sin(z) + rand(1,250);

Create a 3-D scatter plot and fill in the markers. Use view to change the angle of the axes in the figure.

scatter3(x,y,z,'filled')  Get
view(-30,10)

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 5/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

 Set Marker Type

Initialize the random-number generator to make the output of rand repeatable.


Define vectors x and y as cosine and sine values with random noise. Try This Example

 Copy Command

rng default  Get


z = linspace(0,4*pi,250);
x = 2*cos(z) + rand(1,250);
y = 2*sin(z) + rand(1,250);

Create a 3-D scatter plot and set the marker type. Use view to change the angle of the axes in the figure.

figure  Get
scatter3(x,y,z,'*')
view(-30,10)

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 6/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

 Set Marker Properties

Initialize the random-number generator to make the output of rand repeatable.


Define vectors x and y as cosine and sine values with random noise. Try This Example

 Copy Command

rng default  Get


z = linspace(0,4*pi,250);
x = 2*cos(z) + rand(1,250);
y = 2*sin(z) + rand(1,250);

Create a 3-D scatter plot and set the marker edge color and the marker face color. Use view to change the angle of
the axes in the figure.

figure  Get
scatter3(x,y,z,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0 .75 .75])
view(-30,10)

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 7/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

 Plot Data from a Table

Since R2021b
Try This Example
A convenient way to plot data from a table is to pass the table to the scatter3
function and specify the variables you want to plot. For example, read
patients.xls as a table tbl. Plot the relationship between the Systolic,  Copy Command
Diastolic, and Weight variables by passing tbl as the first argument to the
scatter3 function followed by the variable names. By default, the axis labels
match the variable names.

tbl = readtable('patients.xls');  Get


scatter3(tbl,'Systolic','Diastolic','Weight');

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 8/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

You can also plot multiple variables at the same time. For example, plot both blood pressure variables on the x-axis
by specifying the xvar argument as the cell array {'Systolic','Diastolic'}. Then add a legend. The legend
labels match the variable names.

scatter3(tbl,{'Systolic','Diastolic'},'Age','Weight');  Get
legend

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 9/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

 Plot Table Data with Custom Marker Sizes and Colors

Since R2021b
Try This Example
One way to plot data from a table and customize the colors and marker sizes is to
set the ColorVariable and SizeData properties. You can set these properties as
name-value arguments when you call the scatter3 function, or you can set them  Copy Command
on the Scatter object later.

For example, read patients.xls as a table tbl. Plot the relationship between the Systolic, Diastolic, and
Weight variables with filled markers. Vary the marker colors by specifying the ColorVariable name-value
argument. Return the Scatter object as s, so you can set other properties later.

tbl = readtable('patients.xls');  Get


s = scatter3(tbl,'Systolic','Diastolic','Weight','filled', ...
'ColorVariable','Diastolic');

Change the marker sizes to 100 points by setting the SizeData property. Then add a colorbar.

s.SizeData = 100;  Get


colorbar

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 10/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

 Specify Axes for 3-D Scatter Plot

Since R2019b
Try This Example
You can display a tiling of plots using the tiledlayout and nexttile functions.

Load the seamount data set to get vectors x, y, and z. Call the tiledlayout  Copy Command
function to create a 2-by-1 tiled chart layout. Call the nexttile function to create
the axes objects ax1 and ax2. Then create separate scatter plots in the axes by
specifying the axes object as the first argument to scatter3.

load seamount  Get


tiledlayout(2,1)
ax1 = nexttile;
ax2 = nexttile;
scatter3(ax1,x,y,z,'MarkerFaceColor',[0 .75 .75])
scatter3(ax2,x,y,z,'*')

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 11/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

 Set Scatter Series Properties Using Handle

Use the sphere function to create vectors x, y, and z.


Try This Example

 Copy Command

[X,Y,Z] = sphere(16);  Get


x = [0.5*X(:); 0.75*X(:); X(:)];
y = [0.5*Y(:); 0.75*Y(:); Y(:)];
z = [0.5*Z(:); 0.75*Z(:); Z(:)];

Create vectors s and c to specify the size and color for each marker.

S = repmat([70,50,20],numel(X),1);  Get
C = repmat([1,2,3],numel(X),1);
s = S(:);
c = C(:);

Create a 3-D scatter plot and return the scatter series object.

h = scatter3(x,y,z,s,c);  Get

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 12/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Use an RGB triplet color value to set the marker face color. Use dot notation to set properties.

h.MarkerFaceColor = [0 0.5 0.5];  Get

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 13/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Input Arguments collapse all

X — x-coordinates
 scalar | vector | matrix

x-coordinates, specified as a scalar, vector, or matrix. The size and shape of X depends on the shape of your data.
This table describes the most common situations.

Type of Plot How to Specify Coordinates

Single point Specify X, Y, and Z as scalars. For example:

scatter3(1,2,3)

One set of points Specify X, Y, and Z as any combination of row or column vectors of the same length.
For example:

X = [1 2 3 4];
Y = [5; 6; 7; 8];
Z = [9 10 11 12];
scatter3(X,Y,Z)

Multiple sets of points that are If all the data sets share coordinates in one or more dimensions, specify the shared
different colors coordinates as a vector and the other coordinates as matrices. The length of the
vector must match one of the dimensions of the matrices. For example, plot two data
sets that share the same x-coordinates.

X = [1 2 3 4];
Y = [4 5 6 7; 8 9 10 11];
Z = [10 11 12 13; 14 15 16 17];
scatter3(X,Y,Z)

If the matrices are square, scatter3 plots a separate set of points for each column
in the matrices.

Alternatively, specify X, Y, and Z as matrices of equal size. In this case, scatter3


plots the columns of the matrices. For example, plot four data sets.

X = [1 3 5 6; 2 4 6 8];
Y = [10 25 45 61; 20 40 60 70];
Z = [12 5 6 8; 9 13 2 7];
scatter3(X,Y,Z)

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

Y — y-coordinates


https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 14/31
29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

scalar | vector | matrix

y-coordinates, specified as a scalar, vector, or matrix. The size and shape of y depends on the shape of your data.
This table describes the most common situations.

Type of Plot How to Specify Coordinates

Single point Specify X, Y, and Z as scalars. For example:

scatter3(1,2,3)

One set of points Specify X, Y, and Z as any combination of row or column vectors of the same length.
For example:

X = [1 2 3 4];
Y = [5; 6; 7; 8];
Z = [9 10 11 12];
scatter3(X,Y,Z)

Multiple sets of points that are If all the data sets share coordinates in one or more dimensions, specify the shared
different colors coordinates as a vector and the other coordinates as matrices. The length of the
vector must match one of the dimensions of the matrices. For example, plot two data
sets that share the same x-coordinates.

X = [1 2 3 4];
Y = [4 5 6 7; 8 9 10 11];
Z = [10 11 12 13; 14 15 16 17];
scatter3(X,Y,Z)

If the matrices are square, scatter3 plots a separate set of points for each column
in the matrices.

Alternatively, specify X, Y, and Z as matrices of equal size. In this case, scatter3


plots the columns of the matrices. For example, plot four data sets.

X = [1 3 5 6; 2 4 6 8];
Y = [10 25 45 61; 20 40 60 70];
Z = [12 5 6 8; 9 13 2 7];
scatter3(X,Y,Z)

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

Z — z-coordinates
 scalar | vector | matrix

z-coordinates, specified as a scalar, vector, or matrix. The size and shape of Z depends on the shape of your data.
This table describes the most common situations.

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 15/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Type of Plot How to Specify Coordinates

Single point Specify X, Y, and Z as scalars. For example:

scatter3(1,2,3)

One set of points Specify X, Y, and Z as any combination of row or column vectors of the same length.
For example:

X = [1 2 3 4];
Y = [5; 6; 7; 8];
Z = [9 10 11 12];
scatter3(X,Y,Z)

Multiple sets of points that are If all the data sets share coordinates in one or more dimensions, specify the shared
different colors coordinates as a vector and the other coordinates as matrices. The length of the
vector must match one of the dimensions of the matrices. For example, plot two data
sets that share the same x-coordinates.

X = [1 2 3 4];
Y = [4 5 6 7; 8 9 10 11];
Z = [10 11 12 13; 14 15 16 17];
scatter3(X,Y,Z)

If the matrices are square, scatter3 plots a separate set of points for each column
in the matrices.

Alternatively, specify X, Y, and Z as matrices of equal size. In this case, scatter3


plots the columns of the matrices. For example, plot four data sets.

X = [1 3 5 6; 2 4 6 8];
Y = [10 25 45 61; 20 40 60 70];
Z = [12 5 6 8; 9 13 2 7];
scatter3(X,Y,Z)

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

S — Marker size
 36 (default) | numeric scalar | row or column vector | matrix | []

Marker size, specified as a numeric scalar, vector, matrix, or empty array ([]). The size controls the area of each
marker in points squared. An empty array specifies the default size of 36 points. The way you specify the size
depends on how you specify X, Y, and Z and how you want the plot to look. This table describes the most common
situations.

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 16/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Marker Size X, Y, and Z S Example

Same size for all Any valid Scalar Specify X as a vector, Y and Z as matrices, and S as a
points combination of scalar.
vectors or matrices
described for X, Y,
X = [1 2 3 4];
and Z
Y = [5 6 7 8; 9 10 11 12];
Z = [13 14 15 16; 17 18 19 20];
scatter3(X,Y,Z,100)

Different size for Vectors of the same Specify X, Y, Z, and S as vectors.


each point length A vector with the
same length as X,
X = [1 2 3 4];
Y, and Z.
Y = [4 5 6 7];
A matrix with at Z = [8 9 10 11];
least one S = [80 150 700 50];
dimension that scatter3(X,Y,Z,S)
matches the
lengths of X, Y, Specify X, Y, and Z as vectors and S as a matrix.
and Z. Specifying
a matrix is useful
for displaying X = [1 2 3 4];
multiple markers Y = [5 6 7 8];
with different Z = [9 10 11 12];
sizes at each S = [80 30; 150 900; 50 500; 200 350];
(x,y,z) location. scatter3(X,Y,Z,S)

Different size for At least one of X, Y, Specify X as a vector, Y and Z as matrices, and S as a
each point or Z is a matrix for A vector with the vector.
plotting multiple data same number of
sets elements as there
X = [1 2 3 4];
are points in each
Y = [1 6; 3 8; 2 7; 4 9];
data set.
Z = [2 8; 3 10; 4 7; 4 12];
A matrix that has S = [80 150 200 350];
the same size as scatter3(X,Y,Z,S)
the X, Y, or Z
matrix. Specify X as a vector and Y, Z, and S as matrices.

X = [1 2 3 4];
Y = [1 6; 3 8; 2 7; 4 9];
Z = [10 11; 12 13; 14 15; 16 17];
S = [80 30; 150 900; 50 2000; 200 350];
scatter3(X,Y,Z,S)

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

C — Marker color
 color name | RGB triplet | matrix of RGB triplets | vector of colormap indices

Marker color, specified as a color name, RGB triplet, matrix of RGB triplets, or a vector of colormap indices.
https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 17/31
29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Color name — A color name such as "red", or a short name such as "r".

RGB triplet — 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]. RGB triplets
are useful for creating custom colors.

Matrix of RGB triplets — A three-column matrix in which each row is an RGB triplet.

Vector of colormap indices — A vector of numeric values that is the same length as the X, Y, and Z vectors.

The way you specify the color depends on your preferred color scheme and whether you are plotting one set of
coordinates or multiple sets of coordinates. This table describes the most common situations.

Color Scheme How to Specify the Color Example

Use one color for Specify a color name or a short name Plot one set of points, and specify the color as "red".
all the points. from the table below, or specify one
RGB triplet.
X = [1 2 3 4];
Y = [2 5 3 6];
Z = [10 6 4 7];
S = 50;
scatter3(X,Y,Z,S,"red")

Plot two sets of points, and specify the color as red using
the RGB triplet [1 0 0].

X = [1 2 3 4];
Y = [2 5 3 6];
Z = [2 5; 1 2; 8 4; 7 9];
S = 50;
scatter3(X,Y,Z,S,[1 0 0])

Assign different Specify a row or column vector of Create a vector C that specifies four colormap indices. Plot
colors to each numbers. The numbers map into the four points using the colors from the current colormap.
point using a current colormap array. The smallest Then, change the colormap to winter.
colormap. value maps to the first row in the
colormap, and the largest value maps
C = [1 2 3 4];
to the last row. The intermediate
X = [1 2 3 4];
values map linearly to the intermediate
Y = [1 0 6 2];
rows.
Z = [2 5 3 7];
S = 50;
If your plot has three points, specify a
scatter3(X,Y,Z,S,C)
column vector to ensure the values are
colormap(gca,"winter")
interpreted as colormap indices.

You can use this method only when X,


Y, and Z are vectors, and S is either a
vector or a scalar.

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 18/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Color Scheme How to Specify the Color Example

Create a custom Specify an m-by-3 matrix of RGB Create a matrix C that specifies RGB triplets for green, red,
color for each triplets, where m is the number of gray, and purple. Then create a scatter plot of four points
point. points in the plot. using those colors.

You can use this method only when X,


C = [0 1 0; 1 0 0; 0.5 0.5 0.5; 0.6 0 1];
Y, and Z are vectors, and S is either a
X = [1 2 3 4];
vector or a scalar.
Y = [2 5 3 6];
Z = [10 6 4 7];
S = 50;
scatter3(X,Y,Z,S,C)

Create a different Specify an n-by-3 matrix of RGB Create a matrix C that contains two RGB triplets. Then plot
color for each data triplets, where n is the number of data two data sets using those colors.
set. sets.

C = [1 0 0; 0.6 0 1];
You can use this method only when at
X = [1 2 3 4];
least one of X, Y, Z, or S is a matrix.
Y = [5 6 7 8];
Z = [2 5; 1 2; 8 4; 11 9];
S = 50;
scatter3(X,Y,Z,S,C)

Color Names and RGB Triplets for Common Colors

Hexadecimal Color
Color Name Short Name RGB Triplet Appearance
Code

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

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

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 19/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

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"

markertype — Marker
 'o' (default) | '+' | '*' | '.' | 'x' | ...

Marker, specified as one of the markers in this table.

Marker Description Resulting Marker

"o" Circle

"+" Plus sign

"*" Asterisk

"." Point

"x" Cross

"_" Horizontal line

"|" Vertical line

"square" Square

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 20/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Marker Description Resulting Marker

"diamond" Diamond

"^" Upward-pointing triangle

"v" Downward-pointing triangle

">" Right-pointing triangle

"<" Left-pointing triangle

"pentagram" Pentagram

"hexagram" Hexagram

"none" No markers Not applicable

'filled' — Option to fill interior of markers


 'filled'

Option to fill the interior of the markers, specified as 'filled'. Use this option with markers that have a face, for
example, 'o' or 'square'. Markers that do not have a face and contain only edges do not draw ('+', '*', '.', and
'x').

The 'filled' option sets the MarkerFaceColor property of the Scatter object to 'flat' and the
MarkerEdgeColor property to 'none', so the marker faces draw, but the edges do not.

tbl — Source table


 table | timetable

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

xvar — Table variables containing x-coordinates


 one or more table variable indices

Table variables containing the x-coordinates, specified as one or more table variable indices.

Specifying Table Indices

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 21/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Use any of the following indexing schemes to specify the desired variable or variables.

Indexing Scheme Examples

Variable names:
"A" or 'A' — A variable named A
A string, character vector, or cell array.
["A","B"] or {'A','B'} — Two variables named A and
A pattern object. B

"Var"+digitsPattern(1) — Variables named "Var"


followed by a single digit

Variable index:
3 — The third variable from the table
An index number that refers to the location of a variable
in the table. [2 3] — The second and third variables from the table

A vector of numbers. [false false true] — The third variable

A logical vector. Typically, this vector is the same length


as the number of variables, but you can omit trailing 0 or
false values.

Variable type:
vartype("categorical") — All the variables
A vartype subscript that selects variables of a specified containing categorical values
type.

Plotting Your Data


The table variables you specify can contain numeric, categorical, datetime, or duration values.

To plot one data set, specify one variable for xvar, one variable for yvar, and one variable for zvar. For example,
read Patients.xls into the table tbl. Plot the Height, Weight, and Diastolic variables.

tbl = readtable("Patients.xls");
scatter3(tbl,"Height","Weight","Diastolic")

To plot multiple data sets together, specify multiple variables for at least one of xvar, yvar, or zvar. If you specify
multiple variables for more than one argument, the number of variables must be the same for each of those
arguments.

For example, plot the Weight variable on the x-axis, the Systolic and Diastolic variables on the y-axis, and the
Age variable on the z-axis.

scatter3(tbl,"Weight",["Systolic","Diastolic"],"Age")

You can also use different indexing schemes for xvar, yvar, and zvar. For example, specify xvar as a variable
name, yvar as an index number, and zvar as a logical vector.

scatter3(tbl,"Height",6,[false false true])

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 22/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

yvar — Table variables containing y-coordinates


 one or more table variable indices

Table variables containing the y-coordinates, specified as one or more table variable indices.

Specifying Table Indices


Use any of the following indexing schemes to specify the desired variable or variables.

Indexing Scheme Examples

Variable names:
"A" or 'A' — A variable named A
A string, character vector, or cell array.
["A","B"] or {'A','B'} — Two variables named A and
A pattern object. B

"Var"+digitsPattern(1) — Variables named "Var"


followed by a single digit

Variable index:
3 — The third variable from the table
An index number that refers to the location of a variable
in the table. [2 3] — The second and third variables from the table

A vector of numbers. [false false true] — The third variable

A logical vector. Typically, this vector is the same length


as the number of variables, but you can omit trailing 0 or
false values.

Variable type:
vartype("categorical") — All the variables
A vartype subscript that selects variables of a specified containing categorical values
type.

Plotting Your Data


The table variables you specify can contain numeric, categorical, datetime, or duration values.

To plot one data set, specify one variable for xvar, one variable for yvar, and one variable for zvar. For example,
read Patients.xls into the table tbl. Plot the Height, Weight, and Diastolic variables.

tbl = readtable("Patients.xls");
scatter3(tbl,"Height","Weight","Diastolic")

To plot multiple data sets together, specify multiple variables for at least one of xvar, yvar, or zvar. If you specify
multiple variables for more than one argument, the number of variables must be the same for each of those
arguments.

For example, plot the Weight variable on the x-axis, the Systolic and Diastolic variables on the y-axis, and the
Age variable on the z-axis.

scatter3(tbl,"Weight",["Systolic","Diastolic"],"Age")

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 23/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

You can also use different indexing schemes for xvar, yvar, and zvar. For example, specify xvar as a variable
name, yvar as an index number, and zvar as a logical vector.

scatter3(tbl,"Height",6,[false false true])

zvar — Table variables containing z-coordinates


 one or more table variable indices

Table variables containing the z-coordinates, specified as one or more table variable indices.

Specifying Table Indices


Use any of the following indexing schemes to specify the desired variable or variables.

Indexing Scheme Examples

Variable names:
"A" or 'A' — A variable named A
A string, character vector, or cell array.
["A","B"] or {'A','B'} — Two variables named A and
A pattern object. B

"Var"+digitsPattern(1) — Variables named "Var"


followed by a single digit

Variable index:
3 — The third variable from the table
An index number that refers to the location of a variable
in the table. [2 3] — The second and third variables from the table

A vector of numbers. [false false true] — The third variable

A logical vector. Typically, this vector is the same length


as the number of variables, but you can omit trailing 0 or
false values.

Variable type:
vartype("categorical") — All the variables
A vartype subscript that selects variables of a specified containing categorical values
type.

Plotting Your Data


The table variables you specify can contain numeric, categorical, datetime, or duration values.

To plot one data set, specify one variable for xvar, one variable for yvar, and one variable for zvar. For example,
read Patients.xls into the table tbl. Plot the Height, Weight, and Diastolic variables.

tbl = readtable("Patients.xls");
scatter3(tbl,"Height","Weight","Diastolic")

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 24/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

To plot multiple data sets together, specify multiple variables for at least one of xvar, yvar, or zvar. If you specify
multiple variables for more than one argument, the number of variables must be the same for each of those
arguments.

For example, plot the Weight variable on the x-axis, the Systolic and Diastolic variables on the y-axis, and the
Age variable on the z-axis.

scatter3(tbl,"Weight",["Systolic","Diastolic"],"Age")

You can also use different indexing schemes for xvar, yvar, and zvar. For example, specify xvar as a variable
name, yvar as an index number, and zvar as a logical vector.

scatter3(tbl,"Height",6,[false false true])

ax — Axes object
 axes object

Axes object. If you do not specify an axes, then scatter3 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: 'MarkerFaceColor','red' sets the marker face color to red.

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

LineWidth — Width of marker edge


 0.5 (default) | positive value

Width of marker edge, specified as a positive value in point units.

Example: 0.75

MarkerEdgeColor — Marker outline color


 "flat" (default) | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ...

Marker outline color, specified "flat", an RGB triplet, a hexadecimal color code, a color name, or a short name.
The default value of "flat" uses colors from the CData property.

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

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 25/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

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.

Hexadecimal Color
Color Name Short Name RGB Triplet Appearance
Code

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

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 26/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

RGB Triplet Hexadecimal Color Code Appearance

[0.6350 0.0780 0.1840] "#A2142F"

Example: [0.5 0.5 0.5]

Example: "blue"

Example: "#D2F9A7"

MarkerFaceColor — Marker fill color


 "none" (default) | "flat" | "auto" | RGB triplet | hexadecimal color code | "r" | "g" | "b" | ...

Marker fill color, specified as "flat", "auto", an RGB triplet, a hexadecimal color code, a color name, or a short
name. The "flat" option uses the CData values. The "auto" option uses the same color as the Color property for
the axes.

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.

Hexadecimal Color
Color Name Short Name RGB Triplet Appearance
Code

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

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 27/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Hexadecimal Color
Color Name Short Name RGB Triplet Appearance
Code

"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: [0.3 0.2 0.1]

Example: "green"

Example: "#D2F9A7"

ColorVariable — Table variable containing color data


 table variable index

Table variable containing the color data, specified as a variable index into the source table.

Specifying the Table Index


Use any of the following indexing schemes to specify the desired variable.

Indexing Scheme Examples

Variable name:
"A" or 'A' — A variable named A
A string scalar or character vector.
"Var"+digitsPattern(1) — The variable with the
A pattern object. The pattern object must refer to only name "Var" followed by a single digit
one variable.

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 28/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Indexing Scheme Examples

Variable index:
3 — The third variable from the table
An index number that refers to the location of a variable
in the table. [false false true] — The third variable

A logical vector. Typically, this vector is the same length


as the number of variables, but you can omit trailing 0 or
false values.

Variable type:
vartype("double") — The variable containing double
A vartype subscript that selects a table variable of a values
specified type. The subscript must refer to only one
variable.

Specifying Color Data


Specifying the ColorVariable property controls the colors of the markers. The data in the variable controls the
marker fill color when the MarkerFaceColor property is set to "flat". The data can also control the marker outline
color, when the MarkerEdgeColor is set to "flat".

The table variable you specify can contain values of any numeric type. The values can be in either of the following
forms:

A column of numbers that linearly map into the current colormap.

A three-column array of RGB triplets. RGB triplets are three-element vectors whose values specify the intensities
of the red, green, and blue components of specific colors. The intensities must be in the range [0,1]. For
example, [0.5 0.7 1] specifies a shade of light blue.

When you set the ColorVariable property, MATLAB updates the CData property.

Output Arguments collapse all

h — Scatter object
 Scatter object

Scatter object. This is a unique identifier, which you can use to query and modify the properties of the Scatter
object after it is created.

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

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 29/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

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
collapse all
Introduced before R2006a

 R2022b: Plots created with tables preserve special characters in axis and legend labels
When you pass a table and one or more variable names to the scatter3 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 scatter3 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

R2022a

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

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

 R2022a: Plot multiple data sets at once using matrices


The scatter3 function now accepts combinations of vectors and matrices for the coordinates. As a result, you can
visualize multiple data sets at once rather than using the hold function between plotting commands.

 R2021b: Pass tables directly to scatter3


Create plots by passing a table to the scatter3 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
scatter | plot3 | bubblechart3 | swarmchart3

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 30/31


29/12/23, 2:35 3-D scatter plot - MATLAB scatter3 - MathWorks América Latina

Properties
Scatter Properties

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

https://la.mathworks.com/help/matlab/ref/scatter3.html?searchHighlight=plot vectors in 3d&s_tid=srchtitle_support_results_13_plot vectors in 3d 31/31

You might also like