Main Content

scatter3

3-D scatter plot

  • Scatter plot

Description

Vector and Matrix Data

example

scatter3(X,Y,Z)displays circles at the locations specified byX,Y, andZ

  • To plot one set of coordinates, specifyX,Y, andZas vectors of equal length.

  • To plot multiple sets of coordinates on the same set of axes, specify at least one ofX,Y, orZas a matrix.(Since R2022a)

example

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

  • To vary the circle sizes, specifySas 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 specifyingCas"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.

Table Data

example

scatter3(tbl,xvar,yvar,zvar)plots the variablesxvar,yvar, andzvarfrom the tabletbl。To plot one data set, specify one variable each forxvar,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 R2021b)

example

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

Additional Options

example

scatter3(ax,___)plots into the axes specified byaxinstead of into the current axes (gca). Theaxoption 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, seeScatter Properties

example

h= scatter3(___)returns theScatterobject. Usehto modify properties of the scatter chart after it is created.

Examples

collapse all

Create a 3-D scatter plot. Usesphereto define vectorsx,y, andz

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)

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

Usesphereto define vectorsx,y, andz

[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 vectorsto specify the marker sizes.

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

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

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

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

Corresponding entries inx,y,z, andsdetermine the location and size of each marker.

Usesphereto define vectorsx,y, andz

[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 vectorssandcto 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 useviewto change the angle of the axes in the figure.

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

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

Corresponding entries inx,y,z, andcdetermine the location and color of each marker.

Create vectorsxandyas 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. Useviewto change the angle of the axes in the figure.

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

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

Initialize the random-number generator to make the output ofrandrepeatable. Define vectorsxandyas cosine and sine values with random noise.

rngdefaultz = 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. Useviewto change the angle of the axes in the figure.

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

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

Initialize the random-number generator to make the output ofrandrepeatable. Define vectorsxandyas cosine and sine values with random noise.

rngdefaultz = 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. Useviewto change the angle of the axes in the figure.

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

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

Since R2021b

A convenient way to plot data from a table is to pass the table to thescatter3function and specify the variables you want to plot. For example, readpatients.xlsas a tabletbl。Plot the relationship between theSystolic,Diastolic, andWeightvariables by passingtblas the first argument to thescatter3function followed by the variable names. By default, the axis labels match the variable names.

tbl = readtable('patients.xls');scatter3(tbl,'Systolic','Diastolic','Weight');

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

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

scatter3(tbl,{'Systolic','Diastolic'},'Age','Weight');传说

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

Since R2021b

One way to plot data from a table and customize the colors and marker sizes is to set theColorVariableandSizeDataproperties. You can set these properties as name-value arguments when you call thescatter3function, or you can set them on theScatterobject later.

例如,readpatients.xlsas a tabletbl。Plot the relationship between theSystolic,Diastolic, andWeightvariables with filled markers. Vary the marker colors by specifying theColorVariablename-value argument. Return theScatterobject ass, so you can set other properties later.

tbl = readtable('patients.xls');s = scatter3(tbl,'Systolic','Diastolic','Weight','filled',。..'ColorVariable','Diastolic');

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

Change the marker sizes to 100 points by setting theSizeData财产。Then add a colorbar.

s.SizeData = 100; colorbar

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

Starting in R2019b, you can display a tiling of plots using thetiledlayoutandnexttilefunctions.

Load theseamountdata set to get vectorsx,y, andz。Call thetiledlayoutfunction to create a 2-by-1 tiled chart layout. Call thenexttilefunction to create the axes objectsax1andax2。Then create separate scatter plots in the axes by specifying the axes object as the first argument toscatter3

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

Figure contains 2 axes objects. Axes object 1 contains an object of type scatter. Axes object 2 contains an object of type scatter.

Use thespherefunction to create vectorsx,y, andz

[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 vectorssandcto 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);

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

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

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

Input Arguments

collapse all

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

Type of Plot How to Specify Coordinates
Single point

SpecifyX,Y, andZas scalars. For example:

scatter3(1,2,3)

One set of points

SpecifyX,Y, andZas 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 different colors

If all the data sets share coordinates in one or more dimensions, specify the shared 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 samex-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,scatter3plots a separate set of points for each column in the matrices.

Alternatively, specifyX,Y, andZas matrices of equal size. In this case,scatter3plots 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:|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 ofydepends on the shape of your data. This table describes the most common situations.

Type of Plot How to Specify Coordinates
Single point

SpecifyX,Y, andZas scalars. For example:

scatter3(1,2,3)

One set of points

SpecifyX,Y, andZas 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 different colors

If all the data sets share coordinates in one or more dimensions, specify the shared 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 samex-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,scatter3plots a separate set of points for each column in the matrices.

Alternatively, specifyX,Y, andZas matrices of equal size. In this case,scatter3plots 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:|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 ofZdepends on the shape of your data. This table describes the most common situations.

Type of Plot How to Specify Coordinates
Single point

SpecifyX,Y, andZas scalars. For example:

scatter3(1,2,3)

One set of points

SpecifyX,Y, andZas 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 different colors

If all the data sets share coordinates in one or more dimensions, specify the shared 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 samex-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,scatter3plots a separate set of points for each column in the matrices.

Alternatively, specifyX,Y, andZas matrices of equal size. In this case,scatter3plots 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:|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|categorical|datetime|duration

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 specifyX,Y, andZand how you want the plot to look. This table describes the most common situations.

Marker Size X,Y, andZ S Example

Same size for all points

Any valid combination of vectors or matrices described forX,Y, andZ

Scalar

SpecifyXas a vector,YandZas matrices, andSas a scalar.

X = [1 2 3 4]; 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 each point

Vectors of the same length

  • A vector with the same length asX,Y, andZ

  • A matrix with at least one dimension that matches the lengths ofX,Y, andZ。指定一个矩阵是用于显示multiple markers with different sizes at each (x,y,z) location.

SpecifyX,Y,Z, andSas vectors.

X = [1 2 3 4]; Y = [4 5 6 7]; Z = [8 9 10 11]; S = [80 150 700 50]; scatter3(X,Y,Z,S)

SpecifyX,Y, andZas vectors andSas a matrix.

X = [1 2 3 4]; Y = [5 6 7 8]; Z = [9 10 11 12]; S = [80 30; 150 900; 50 500; 200 350]; scatter3(X,Y,Z,S)

Different size for each point

At least one ofX,Y, orZis a matrix for plotting multiple data sets

  • A vector with the same number of elements as there are points in each data set.

  • A matrix that has the same size as theX,Y, orZmatrix.

SpecifyXas a vector,YandZas matrices, andSas a vector.

X = [1 2 3 4]; Y = [1 6; 3 8; 2 7; 4 9]; Z = [2 8; 3 10; 4 7; 4 12]; S = [80 150 200 350]; scatter3(X,Y,Z,S)

SpecifyXas a vector andY,Z, andSas 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:|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

标志的颜色,颜色名称,指定RGB triplet, matrix of RGB triplets, or a vector of colormap indices.

  • 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 theX,Y, andZvectors.

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 all the points.

Specify a color name or a short name from the table below, or specify one RGB triplet.

Plot one set of points, and specify the color as"red"

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 colors to each point using a colormap.

Specify a row or column vector of numbers. The numbers map into the current colormap array. The smallest value maps to the first row in the colormap, and the largest value maps to the last row. The intermediate values map linearly to the intermediate rows.

If your plot has three points, specify a column vector to ensure the values are interpreted as colormap indices.

You can use this method only whenX,Y, andZare vectors, andSis either a vector or a scalar.

Create a vectorCthat specifies four colormap indices. Plot four points using the colors from the current colormap. Then, change the colormap towinter

C = [1 2 3 4]; X = [1 2 3 4]; Y = [1 0 6 2]; Z = [2 5 3 7]; S = 50; scatter3(X,Y,Z,S,C) colormap(gca,"winter")

fo创建一个自定义颜色r each point.

Specify an m-by-3 matrix of RGB triplets, where m is the number of points in the plot.

You can use this method only whenX,Y, andZare vectors, andSis either a vector or a scalar.

Create a matrixCthat specifies RGB triplets for green, red, gray, and purple. Then create a scatter plot of four points using those colors.

C = [0 1 0; 1 0 0; 0.5 0.5 0.5; 0.6 0 1]; X = [1 2 3 4]; Y = [2 5 3 6]; Z = [10 6 4 7]; S = 50; scatter3(X,Y,Z,S,C)

Create a different color for each data set.

Specify an n-by-3 matrix of RGB triplets, where n is the number of data sets.

You can use this method only when at least one ofX,Y,Z, orSis a matrix.

Create a matrixCthat contains two RGB triplets. Then plot two data sets using those colors.

C = [1 0 0; 0.6 0 1]; X = [1 2 3 4]; 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

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

“白色” 'w' (1 1 1) '#FFFFFF'

Sample of the color white

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'

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980] '#D95319'

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250] '#EDB120'

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560] '#7E2F8E'

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880] '#77AC30'

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330] '#4DBEEE'

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840] '#A2142F'

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

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

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

's' Square

Sample of square marker

'd' Diamond

Sample of diamond line 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

'p' Pentagram

Sample of pentagram marker

'h' Hexagram

Sample of hexagram marker

'none' No markers Not 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 theMarkerFaceColorproperty of theScatterobject to'flat'and theMarkerEdgeColorproperty to'none', so the marker faces draw, but the edges do not.

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

Table variables containing thex-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.

索引Scheme Examples

Variable names:

  • A string, character vector, or cell array.

  • Apatternobject.

  • "A"or'A'- - - - - -A variable calledA

  • ["A","B"]or{'A','B'}- - - - - -Two variables calledAandB

  • "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 trailing0orfalsevalues.

  • 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:

  • Avartypesubscript that selects variables of a specified type.

  • vartype("categorical")- - - - - -All the variables containing categorical values

Plotting Your Data

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

To plot one data set, specify one variable forxvar, one variable foryvar, and one variable forzvar。例如,readPatients.xlsinto the tabletbl。Plot theHeight,Weight, andDiastolicvariables.

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

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

例如,plot theWeightvariable on thex-axis, theSystolicandDiastolicvariables on they-axis, and theAgevariable on thez-axis.

scatter3(tbl,'Weight',{'Systolic','Diastolic'},'Age')

You can also use different indexing schemes forxvar,yvar, andzvar。例如,specifyxvaras a variable name,yvaras an index number, andzvaras a logical vector.

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

Table variables containing they-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.

索引Scheme Examples

Variable names:

  • A string, character vector, or cell array.

  • Apatternobject.

  • "A"or'A'- - - - - -A variable calledA

  • ["A","B"]or{'A','B'}- - - - - -Two variables calledAandB

  • "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 trailing0orfalsevalues.

  • 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:

  • Avartypesubscript that selects variables of a specified type.

  • vartype("categorical")- - - - - -All the variables containing categorical values

Plotting Your Data

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

To plot one data set, specify one variable forxvar, one variable foryvar, and one variable forzvar。例如,readPatients.xlsinto the tabletbl。Plot theHeight,Weight, andDiastolicvariables.

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

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

例如,plot theWeightvariable on thex-axis, theSystolicandDiastolicvariables on they-axis, and theAgevariable on thez-axis.

scatter3(tbl,'Weight',{'Systolic','Diastolic'},'Age')

You can also use different indexing schemes forxvar,yvar, andzvar。例如,specifyxvaras a variable name,yvaras an index number, andzvaras a logical vector.

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

Table variables containing thez-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.

索引Scheme Examples

Variable names:

  • A string, character vector, or cell array.

  • Apatternobject.

  • "A"or'A'- - - - - -A variable calledA

  • ["A","B"]or{'A','B'}- - - - - -Two variables calledAandB

  • "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 trailing0orfalsevalues.

  • 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:

  • Avartypesubscript that selects variables of a specified type.

  • vartype("categorical")- - - - - -All the variables containing categorical values

Plotting Your Data

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

To plot one data set, specify one variable forxvar, one variable foryvar, and one variable forzvar。例如,readPatients.xlsinto the tabletbl。Plot theHeight,Weight, andDiastolicvariables.

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

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

例如,plot theWeightvariable on thex-axis, theSystolicandDiastolicvariables on they-axis, and theAgevariable on thez-axis.

scatter3(tbl,'Weight',{'Systolic','Diastolic'},'Age')

You can also use different indexing schemes forxvar,yvar, andzvar。例如,specifyxvaras a variable name,yvaras an index number, andzvaras a logical vector.

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

Axes object. If you do not specify an axes, thenscatter3plots into the current axes.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis 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 encloseNamein quotes.

Example:'MarkerFaceColor','red'sets the marker face color to red.

The properties listed here are only a subset. For a complete list, seeScatter 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 theCData财产。

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 from0toF。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 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

“白色” 'w' (1 1 1) '#FFFFFF'

Sample of the color white

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

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980] '#D95319'

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250] '#EDB120'

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560] '#7E2F8E'

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880] '#77AC30'

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330] '#4DBEEE'

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840] '#A2142F'

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

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 theCDatavalues. The'auto'option uses the same color as theColorproperty 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 from0toF。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 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

“白色” 'w' (1 1 1) '#FFFFFF'

Sample of the color white

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

Sample of RGB triplet [0 0.4470 0.7410], which appears as dark blue

[0.8500 0.3250 0.0980] '#D95319'

Sample of RGB triplet [0.8500 0.3250 0.0980], which appears as dark orange

[0.9290 0.6940 0.1250] '#EDB120'

Sample of RGB triplet [0.9290 0.6940 0.1250], which appears as dark yellow

[0.4940 0.1840 0.5560] '#7E2F8E'

Sample of RGB triplet [0.4940 0.1840 0.5560], which appears as dark purple

[0.4660 0.6740 0.1880] '#77AC30'

Sample of RGB triplet [0.4660 0.6740 0.1880], which appears as medium green

[0.3010 0.7450 0.9330] '#4DBEEE'

Sample of RGB triplet [0.3010 0.7450 0.9330], which appears as light blue

[0.6350 0.0780 0.1840] '#A2142F'

Sample of RGB triplet [0.6350 0.0780 0.1840], which appears as dark red

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.

索引Scheme Examples

Variable name:

  • A string scalar or character vector.

  • Apatternobject. The pattern object must refer to only one variable.

  • "A"or'A'- - - - - -A variable calledA

  • "Var"+digitsPattern(1)- - - - - -The variable with the name"Var"followed by a single digit

Variable index:

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

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing0orfalsevalues.

  • 3- - - - - -The third variable from the table

  • [false false true]- - - - - -The third variable

Variable type:

  • Avartypesubscript that selects a table variable of a specified type. The subscript must refer to only one variable.

  • vartype("double")- - - - - -The variable containing double values

Specifying Color Data

Specifying theColorVariableproperty controls the colors of the markers. The data in the variable controls the marker fill color when theMarkerFaceColorproperty is set to'flat'。The data can also control the marker outline color, when theMarkerEdgeColoris 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]。例如,[0.5 0.7 1]specifies a shade of light blue.

When you set theColorVariableproperty, MATLAB updates theCData财产。

Output Arguments

collapse all

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

Extended Capabilities

Version History

Introduced before R2006a

expand all