scatter3

3-D scatter plot

• Description

Vector and Matrix Data

example

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

example

scatter3(X,Y,Z,S) draws each circle with the size specified by S. To plot each circle with equal size, specify S as a scalar. To plot each circle with a specific size, specify S as a vector.

example

scatter3(X,Y,Z,S,C) draws each circle with the color specified by C.

• If C is a RGB triplet or character vector or string containing a color name, then all circles are plotted with the specified color.

• If C is a three column matrix with the number of rows in C equal to the length of X, Y, and Z, then each row of C specifies an RGB color value for the corresponding circle.

• If C is a vector with length equal to the length of X, Y, and Z, then the values in C are linearly mapped to the colors in the current colormap.

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.

Table Data

example

scatter3(tbl,xvar,yvar,zvar) plots the variables xvar, yvar, and zvar from the table tbl. To plot 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)

example

scatter3(ax,___) plots into the axes specified by ax instead of into the current axes (gca). The ax 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 a 3-D scatter plot. Use sphere to define vectors x, y, and z.

figure
[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) Use sphere to define vectors x, y, and z.

[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(:)];

Define vector s to specify the marker sizes.

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

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

figure
scatter3(x,y,z,s)
view(40,35) Corresponding entries in x, y, z, and s determine the location and size of each marker.

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

[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(:)];

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

S = repmat([50,25,10],numel(X),1);
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.

figure
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.

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

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 fill in the markers. Use view to change the angle of the axes in the figure.

scatter3(x,y,z,'filled')
view(-30,10) 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.

rng default
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
scatter3(x,y,z,'*')
view(-30,10) 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.

rng default
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
scatter3(x,y,z,...
'MarkerEdgeColor','k',...
'MarkerFaceColor',[0 .75 .75])
view(-30,10) Since R2021b

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, 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.

scatter3(tbl,'Systolic','Diastolic','Weight'); 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');
legend Since R2021b

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

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;
colorbar Starting in R2019b, 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 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.

tiledlayout(2,1)
ax1 = nexttile;
ax2 = nexttile;
scatter3(ax1,x,y,z,'MarkerFaceColor',[0 .75 .75])
scatter3(ax2,x,y,z,'*') Use the sphere function to create vectors x, y, and z.

[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(:)];

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

S = repmat([70,50,20],numel(X),1);
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); 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]; Input Arguments

collapse all

x values, specified as a vector. X, Y, and Z must be vectors of equal length.

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

y values, specified as a vector. X, Y, and Z must be vectors of equal length.

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

z values, specified as a vector. X, Y, and Z must be vectors of equal length.

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

Marker area, specified as a scalar, a vector, or []. The values in S must be positive. The units for area are points squared.

• If S is a scalar, then scatter3 plots all markers with the specified area.

• If S is a row or column vector, then each entry in S specifies the area for the corresponding marker. The length of S must equal the length of X, Y and Z. Corresponding entries in X, Y, Z and S determine the location and area of each marker.

• If S is empty, then the default size of 36 points squared is used.

Example: 50

Example: [36,25,25,17,46]

Marker color, specified as an RGB triplet, a three-column matrix of RGB triplet, a vector, or one of the color options in the table.

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]. Alternatively, you can specify some common colors by name. This table lists the long and short color name options and the equivalent RGB triplet values.

OptionDescriptionEquivalent RGB Triplet
'red' or 'r'Red[1 0 0]
'green' or 'g'Green[0 1 0]
'blue' or 'b'Blue[0 0 1]
'yellow' or 'y'Yellow[1 1 0]
'magenta' or 'm'Magenta[1 0 1]
'cyan' or 'c'Cyan[0 1 1]
'white' or 'w'White[1 1 1]
'black' or 'k'Black[0 0 0]

If you have three points in the scatter plot and want the colors to be indices into the colormap, specify C as a three-element column vector.

Example: 'y'

Example: [1,2,3,4]

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

MarkerDescriptionResulting Marker
'o'Circle '+'Plus sign '*'Asterisk '.'Point 'x'Cross '_'Horizontal line '|'Vertical line 's'Square 'd'Diamond '^'Upward-pointing triangle 'v'Downward-pointing triangle '>'Right-pointing triangle '<'Left-pointing triangle 'p'Pentagram 'h'Hexagram 'none'No markersNot applicable

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.

Source table containing the data to plot. Specify this argument as a table or a timetable.

Table variables containing the x-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 SchemeExamples

Variable names:

• A character vector or a string scalar.

• A cell array or string vector.

• 'A' or "A" — A variable called A

• {'A','B'} or ["A","B"] — Two variables called A and B

Variable numbers:

• An index number that refers to the location of a variable in the table.

• A vector of numbers.

• 3 — The third variable from the table

• [2 3] — The second and third variables from the table

Logical vector:

• An n-element vector logical vector, where logical 1 (true) values indicate the desired variables.

• Optionally, omit the trailing false values after the last true value.

• [false false true false] — The third variable from an m-by-4 table

• [false false true] — The third variable from any size table

• [false true true] — The second and third variables from any size table

Variable type:

• A vartype command that selects table 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.

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.

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])

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 SchemeExamples

Variable names:

• A character vector or a string scalar.

• A cell array or string vector.

• 'A' or "A" — A variable called A

• {'A','B'} or ["A","B"] — Two variables called A and B

Variable numbers:

• An index number that refers to the location of a variable in the table.

• A vector of numbers.

• 3 — The third variable from the table

• [2 3] — The second and third variables from the table

Logical vector:

• An n-element vector logical vector, where logical 1 (true) values indicate the desired variables.

• Optionally, omit the trailing false values after the last true value.

• [false false true false] — The third variable from an m-by-4 table

• [false false true] — The third variable from any size table

• [false true true] — The second and third variables from any size table

Variable type:

• A vartype command that selects table 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.

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.

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])

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 SchemeExamples

Variable names:

• A character vector or a string scalar.

• A cell array or string vector.

• 'A' or "A" — A variable called A

• {'A','B'} or ["A","B"] — Two variables called A and B

Variable numbers:

• An index number that refers to the location of a variable in the table.

• A vector of numbers.

• 3 — The third variable from the table

• [2 3] — The second and third variables from the table

Logical vector:

• An n-element vector logical vector, where logical 1 (true) values indicate the desired variables.

• Optionally, omit the trailing false values after the last true value.

• [false false true false] — The third variable from an m-by-4 table

• [false false true] — The third variable from any size table

• [false true true] — The second and third variables from any size table

Variable type:

• A vartype command that selects table 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.

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.

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])

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

Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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.

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

Example: 0.75

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.

• 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 character vector or a string scalar 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. Thus, 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' '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 applicableNot applicableNot applicableNo color

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

[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.5 0.5 0.5]

Example: 'blue'

Example: '#D2F9A7'

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 character vector or a string scalar 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. Thus, 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' '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 applicableNot applicableNot applicableNo color

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

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

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 SchemeExamples

Variable name:

A character vector or a string scalar.

• 'A' or "A" — A variable called A

Variable number:

An index number that refers to the location of a variable in the table.

• 3 — The third variable from the table

Logical vector:

• An n-element vector logical vector, where logical 1 (true) values indicate the desired variable.

• Optionally, omit the trailing false values after the true value.

• [false false true false] — The third variable from an m-by-4 table

• [false false true] — The third variable from any size table

Variable type:

A vartype command that selects table variables of a specified type.

• vartype('double') — The variable containing double values

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

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.