scatter

Scatter plot

Syntax

Description

example

scatter(x,y) creates a scatter plot with 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,a) specifies the circle areas. To plot each circle with equal area, specify a as a scalar. To plot each circle with a different area, specify a as a vector with length equal to the length of x and y.

example

scatter(x,y,a,c) specifies the circle colors. To plot all circles with the same color, specify c as a single color string or an RGB triplet. To use varying color, specify c as a vector or a three-column matrix of RGB triplets.

example

scatter(___,'filled') fills in the circles. Use the 'filled' option with any of the input argument combinations in the previous syntaxes.

example

scatter(___,markertype) specifies the marker type.

example

scatter(___,Name,Value) specifies scatter series properties using one or more Name,Value pair arguments. For example, 'LineWidth',2 sets the marker outline width to 2 points.

example

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

example

s = scatter(___) returns the scatter series object. Use s to make future modifications to the scatter series after it is created.

Examples

expand all

Create Scatter Plot

Create x as 200 equally spaced values between 0 and $3\pi$. Create y as cosine values with random noise. Then, create a scatter plot.

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

Vary Circle Size

Create a scatter plot using circles with different areas.

x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
a = linspace(1,100,200);
scatter(x,y,a)

Corresponding elements in x, y, and a determine the location and area of each circle. To plot all circles with the equal area, specify a as a numeric scalar.

Vary Circle Color

Create a scatter plot and vary the circle color.

x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],c)

Corresponding elements in x, y, and c determine the location and color of each circle. The scatter function maps the elements in c to colors in the current colormap.

Fill the Markers

Create a scatter plot and fill in the markers. scatter fills each marker using the color of the marker edge.

x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
a = 25;
c = linspace(1,10,length(x));
scatter(x,y,a,c,'filled')

Specify Marker Symbol

Create vectors x and y as sine and cosine values with random noise. Then, create a scatter plot and use diamond markers with an area of 140 points squared.

theta = linspace(0,2*pi,150);
x = sin(theta) + 0.75*rand(1,150);
y = cos(theta) + 0.75*rand(1,150);
a = 140;
scatter(x,y,a,'d')

Change Marker Color and Line Width

Create vectors x and y as sine and cosine values with random noise. Create a scatter plot and set the marker edge color, marker face color, and line width.

theta = linspace(0,2*pi,300);
x = sin(theta) + 0.75*rand(1,300);
y = cos(theta) + 0.75*rand(1,300);
a = 40;
scatter(x,y,a,'MarkerEdgeColor',[0 .5 .5],...
              'MarkerFaceColor',[0 .7 .7],...
              'LineWidth',1.5)

Specify Subplot for Scatter Plot

Create a figure with two subplots and add a scatter plot to each subplot. Use filled diamond markers for the scatter plot in the lower subplot.

x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
ax1 = subplot(2,1,1);
scatter(ax1,x,y)

ax2 = subplot(2,1,2);
scatter(ax2,x,y,'filled','d')

Modify Scatter Series After Creation

Create a scatter plot and return the scatter series object, s.

theta = linspace(0,1,500);
x = exp(theta).*sin(100*theta);
y = exp(theta).*cos(100*theta);
s = scatter(x,y);

Use s to query and set properties of the scatter series after it has been created. Set the line width to 0.6 point. Set the marker edge color to blue. Set the marker face color using an RGB triplet color. Starting in R2014b, you can use dot notation to set properties. If you are using an earlier release, use the set function instead.

s.LineWidth = 0.6;
s.MarkerEdgeColor = 'b';
s.MarkerFaceColor = [0 0.5 0.5];

Input Arguments

expand all

x — x valuesvector

x values, 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 — y valuesvector

y values, 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

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

Marker area, specified in one of these forms:

  • Numeric scalar — Plot all markers with equal area.

  • Row or column vector — Use different areas for each marker. Corresponding elements in x, y, and a determine the location and area of each marker. The length of a must equal the length of x and y.

  • [] — Use the default area of 36 points squared.

The units for the marker area is points squared.

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] (default) | color string | RGB triplet | three-column matrix of RGB triplets | vector

Marker color, specified in one of these forms:

  • Color string or RGB triplet — Plot all markers with the same color.

  • Three column matrix of RGB triplets — Use different colors for each marker. Each row of the matrix specifies an RGB triplet color for the corresponding marker. The number of rows must equal the length of x and y.

  • Vector — Use different colors for each marker and linearly map values in c to the colors in the current colormap. The length of c must equal the length of x and y. To change the colormap for the axes, use the colormap function.

    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.

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

Example: [1 2 3 4]

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

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

Marker type, specified as one of the strings listed in this table.

StringMarker Symbol
'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 markers

ax — Axes objectaxes object

Axes object. If you do not specify an axes, then the scatter function uses 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 scatter series properties listed here are only a subset. For a complete list, see Scatter Series Properties.

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

'LineWidth' — Width of marker edge0.5 (default) | positive value

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

Example: 0.75

Output Arguments

expand all

s — Scatter series objectscatter series object

Scatter series object. Use s to access and modify properties of the scatter series after it has been created.

See Also

Functions

Properties

Was this topic helpful?