# scatter3

3-D scatter plot

## Syntax

• `scatter3(X,Y,Z)` example
• `scatter3(X,Y,Z,S)` example
• `scatter3(X,Y,Z,S,C)` example
• `scatter3(___,'filled')` example
• `scatter3(___,markertype)` example
• `scatter3(___,Name,Value)` example
• `scatter3(ax,___)` example
• `h = scatter3(___)` example

## Description

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 color string or an RGB row vector, 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.```

example

````scatter3(___,Name,Value)` specifies scatter series properties using one or more `Name,Value` pair arguments. ```

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

````h = scatter3(___)` returns the scatter series object. Use `h` to modify properties of the scatter series 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`.

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

### Vary Marker Size

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.

### Vary Marker Color

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.

### Fill in Markers

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

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

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

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

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

### Specify Axes for 3-D Scatter Plot

Load the `seamount` data set to get vectors `x`, `y`, and `z`.

```load seamount ```

Create a figure with two subplots and return the handles to the two axes in array `hs`. In each subplot, create a 3-D scatter plot. Specify the marker properties for each scatter plot.

```figure hs(1) = subplot(2,1,1); hs(2) = subplot(2,1,2); scatter3(hs(1),x,y,z,'MarkerFaceColor',[0 .75 .75]) scatter3(hs(2),x,y,z,'*') ```

### Set Scatter Series Properties Using Handle

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. Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the `set` function instead.

```h.MarkerFaceColor = [0 0.5 0.5]; ```

## Input Arguments

collapse all

### `X` — x valuesvector

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` | `logical`

### `Y` — y values (default) | vector

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` | `logical`

### `Z` — z values (default) | vector

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` | `logical`

### `S` — Marker area36 (default) | scalar | vector | []

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

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

### `C` — Marker color[0 0 1] (blue) (default) | color string | RGB row vector | three-column matrix of RGB values | vector

Marker color, specified as a color string, an RGB row vector, a three-column matrix of RGB values, or a vector. For an RGB row vector, use 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]`. 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.

This table lists the predefined colors and their RGB equivalents.

RGB Vector

Short Name

Long Name

`[1 1 0]`

'`y`'

'`yellow`'

`[1 0 1]`

'`m`'

'`magenta`'

`[0 1 1]`

'`c`'

'`cyan`'

`[1 0 0]`

'`r`'

'`red`'

`[0 1 0]`

'`g`'

'`green`'

`[0 0 1]`

'`b`'

'`blue`'

`[1 1 1]`

'`w`'

'`white`'

`[0 0 0]`

'`k`'

'`black`'

Example: `'y'`

Example: `[1,2,3,4]`

Example: `reshape([0,1,0,0,0,1,0.5,1,0.2],3,3)`

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

### `markertype` — Marker type`'o'` (default) | string

Marker type, specified as a string. The table below lists the supported marker types.

Specifier

Marker Type

'`o`'

Circle

'`+`'

Plus sign

'`*`'

Asterisk

'`.`'

Point

'`x`'

Cross

'`square`' or '`s`'

Square

'`diamond`' or '`d`'

Diamond

'`^`'

Upward-pointing triangle

'`v`'

Downward-pointing triangle

'`>`'

Right-pointing triangle

'`<`'

Left-pointing triangle

'`pentagram`' or '`p`'

Five-pointed star (pentagram)

'`hexagram`' or '`h`'

Six-pointed star (hexagram)

'`none`'

No marker

### `ax` — Axes objectaxes object

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

### Name-Value Pair 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 single 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 Series 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) | `'none'` | RGB triplet | color string

Marker outline color, specified as one of these values:

• `'flat'` — Colors defined by the `CData` property.

• `'none'` — No color, which makes unfilled markers invisible.

• RGB triplet or color string — Specify a custom color.

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]`. This table lists RGB triplet values that have equivalent color strings.

Long NameShort NameRGB Triplet
`'yellow'``'y'``[1 1 0]`
`'magenta'``'m'``[1 0 1]`
`'cyan'``'c'``[0 1 1]`
`'red'``'r'``[1 0 0]`
`'green'``'g'``[0 1 0]`
`'blue'``'b'``[0 0 1]`
`'white'``'w'``[1 1 1]`
`'black``'k'``[0 0 0]`

Example: `[0.5 0.5 0.5]`

Example: `'blue'`

### `'MarkerFaceColor'` — Marker fill color`'none'` (default) | `'flat'` | `'auto'` | RGB triplet | color string

Marker fill color, specified as one of these values:

• `'none'` — No color, which makes the interior invisible.

• `'flat'` — Colors defined by the `CData` property.

• `'auto'` — Same color as the `Color` property for the axes.

• RGB triplet or color string — Specify a custom color.

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]`. This table lists RGB triplet values that have equivalent color strings.

Long NameShort NameRGB Triplet
`'yellow'``'y'``[1 1 0]`
`'magenta'``'m'``[1 0 1]`
`'cyan'``'c'``[0 1 1]`
`'red'``'r'``[1 0 0]`
`'green'``'g'``[0 1 0]`
`'blue'``'b'``[0 0 1]`
`'white'``'w'``[1 1 1]`
`'black``'k'``[0 0 0]`

Example: `[0.3 0.2 0.1]`

Example: `'green'`

## Output Arguments

collapse all

### `h` — Scatter series objectscalar

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