Documentation Center

  • Trial Software
  • Product Updates

scatter

Scatter plot

Syntax

Description

example

scatter(X,Y) displays circles at the locations specified by the vectors X and Y. This type of graph is also known as a bubble plot.

example

scatter(X,Y,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 with length equal to the length of X and Y.

example

scatter(X,Y,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 and Y, 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 and Y, then the values in C are linearly mapped to the colors in the current colormap.

example

scatter(___,'fill') fills in the circles, using any of the input argument combinations in the previous syntaxes.

example

scatter(___,markertype) specifies the marker type.

example

scatter(___,Name,Value) specifies scattergroup property settings using one or more Name,Value pair arguments.

example

scatter(axes_handle,___) plots into the axes specified by axes_handle instead of into the current axes (gca). The axes_handle option can precede any of the input argument combinations in the previous syntaxes.

example

h = scatter(___) returns the scattergroup object handle, h.

Examples

expand all

Create Scatter Plot

Define x to contain 200 equally spaced values between 0 and $3\pi$ . Initialize the random-number generator to make the output of rand repeatable, and define y to contain cosine values with random noise.

x = linspace(0,3*pi,200);
rng(0,'twister');
y = cos(x)+ rand(1,200);

Create a scatter plot using the two vector inputs.

figure
scatter(x,y)

scatter plots entries in x against corresponding entries in y.

Vary Circle Size

Load the sample data from seamount to get vectors x and y. Define s as a vector of linearly spaced values between 1 and 50 that is the same length as x. Create a scatter plot and vary the circle size. Use zoom to zoom in on the scatter plot.

figure
load seamount
s = linspace(1,50,length(x));
scatter(x,y,s)
zoom(2)

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

Vary Circle Color

Load the sample data from seamount to get vectors x and y. Define c as a vector of linearly spaced values between 1 and 10 that is the same length as x. Create a scatter plot and vary the circle color. Use zoom to zoom in on the scatter plot.

figure
load seamount
s = 10;
c = linspace(1,10,length(x));
scatter(x,y,s,c)
zoom(2)

Corresponding entries in x, y, and c determine the location and color of each marker. Since s is a scalar value, all markers are the same size.

Vary Circle Size and Color

Load sample data from seamount to get vectors x, y, and z. Create a scatter plot and vary the circle size and color. Fill in the circles and use zoom to zoom in on the scatter plot.

figure
load seamount
s = sqrt(-z/2);
c = z;
scatter(x,y,s,c,'fill')
zoom(2)

scatter plots each circle with a specific size and color determined by the vectors s and c, respectively.

Specify Marker Symbol

Initialize the random-number generator to make the output of rand repeatable. Set up vectors x and y as sine and cosine values with random noise.

rng(0,'twister');
theta = linspace(0,2*pi,150);
x = sin(theta) + 0.75*rand(1,150);
y = cos(theta) + 0.75*rand(1,150);

Create a scatter plot and set the marker type to diamonds with an area of 140 points squared.

figure
s = 140;
scatter(x,y,s,'d')

To fill in the diamonds, use the 'fill' option.

Specify Marker Properties

Initialize the random-number generator to make the output of rand repeatable. Define vectors x and y as sine and cosine values with random noise.

rng(0,'twister');
theta = linspace(0,2*pi,300);
x = sin(theta) + 0.75*rand(1,300);
y = cos(theta) + 0.75*rand(1,300);

Create a scatter plot and set the marker size, edge color, face color, and line width using Name,Value pair arguments.

figure
s = 40;
scatter(x,y,s,'MarkerEdgeColor','b',...
              'MarkerFaceColor','c',...
              'LineWidth',1.5)

Specify Scatter Plot Axes

Load the sample data from seamount to get vectors x, y, and z.

load seamount

Create a figure with two subplots and return the handles to the two subplot axes in array hs. Create a scatter plot in the upper subplot using the axes handle, hs(1). In the lower subplot, create another scatter plot from the same data sample and use filled, diamond markers.

figure
hs(1) = subplot(1,2,1);
hs(2) = subplot(1,2,2);
s = 30;
c = z;
scatter(hs(1),x,y,s,c)
scatter(hs(2),x,y,s,c,'fill','d')

Set Marker Properties Using the Handle

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 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 scattergroup handle, h.

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

Use the handle to set the marker face color for the scatter plot.

set(h,'MarkerFaceColor',[0 .5 .5])

Input Arguments

expand all

X — Value of data to display on x-axisvector

Value of data to display on the x-axis, specified as a vector. X and Y must be vectors of equal length.

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

Y — Value of data to display on y-axisvector

Value of data to display on the y-axis, specified as a vector. X and Y must be vectors of equal length.

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

S — Marker area36 (default) | scalar | row or column vector | []

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

  • If S is a scalar, then scatter 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 and Y, and corresponding entries in X, Y, 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: 'k'

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

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

Marker type, specified as a string. This table 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

Example: 'p'

Data Types: char

axes_handle — Axes handlehandle

Axes handle, which is the reference to an axes object. Use the gca function to get the handle to the current axes, for example, axes_handle = gca;.

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.

For more information on these settings see scattergroup.

'LineWidth' — Line width0.5 (default) | scalar

Line width, specified as the comma-separated pair consisting of 'LineWidth' and a scalar in points. The scalar sets the width size of the marker edge.

Example: 'LineWidth',0.75

'MarkerEdgeColor' — Marker edge color'flat' (default) | 'none' | 'auto' | three-element RGB vector | string

Marker edge color, specified as the comma-separated pair consisting of 'MarkerEdgeColor' and a color value. The color value can be one of the supported strings or an RGB vector, listed in the following tables.

Specifier

Result

'flat'Sets the color using the CData property.

'auto'

Sets the color to the axes Color property. If the axes Color property is none, then sets the color to the figure Color property.

'none'

Specifies no color, which makes nonfilled markers invisible.

For an RGB 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]. This table lists the predefined string 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: 'MarkerEdgeColor',[1 .8 .1]

'MarkerFaceColor' — Marker face color'none' (default) | 'auto' | 'flat' | three-element RGB vector | string

Marker face color, specified as the comma-separated pair consisting of 'MarkerFaceColor' and a color value. MarkerFaceColor sets the fill color for markers that are closed shapes (circle, square, diamond, pentagram, hexagram, and the four triangles). The color value can be one of the supported strings or an RGB vector, listed in the following tables.

Specifier

Result

'flat'Sets the fill color using the CData property.

'auto'

Sets the fill color to the axes Color property. If the axes Color property is none, then sets the fill color to the figure Color property.

'none'

Makes the interior of the marker transparent, allowing the background to show through.

For an RGB 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]. This table lists the predefined string 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: 'MarkerFaceColor',[0 .8 1]

Output Arguments

expand all

h — Scattergroup object handlescalar

Scattergroup object handle, returned as a scalar. This is a unique identifier, which you can use to query and modify the properties of the scattergroup object.

See Also

| | | |

Was this topic helpful?