gscatter(x,y,g,clr,sym,siz,doleg,xnam,ynam)
specifies the names to use for the x-axis and y-axis labels. If you do not
provide xnam and ynam, and the
x and y inputs are variables with
names, then gscatter labels the axes with the variable
names.

gscatter(ax,___) uses
the plot axes specified by the axes object ax. Specify
ax as the first input argument followed by any of the
input argument combinations in the previous syntaxes.

Plot the Displacement values on the x-axis and the Horsepower values on the y-axis. gscatter uses the variable names as the default labels for the axes. Group the data points by Model_Year.

The data set contains ratings of cities according to nine factors such as climate, housing, education, and health. The matrix ratings contains the ratings information.

Plot the relationship between the ratings for climate (first column) and housing (second column) grouped by city size in the matrix group. Choose different colors and plotting symbols for each group.

Plot the ages and weights of the hospital patients. Group the patients according to their gender and smoker status. Use the o symbol to represent nonsmokers and the * symbol to represent smokers.

x = hospital.Age;
y = hospital.Weight;
g = {hospital.Sex,hospital.Smoker};
gscatter(x,y,g,'rkgb','o*',8,'on','Age','Weight')
legend('Location','northeastoutside')

Load the carsmall data set. Create a figure with two subplots and return the axes objects as ax1 and ax2. Create a scatter plot in each set of axes by referring to the corresponding Axes object. In the left subplot, group the data using the Model_Year variable. In the right subplot, group the data using the Cylinders variable. Add a title to each plot by passing the corresponding Axes object to the title function.

load carsmall
color = lines(6); % Generate color values
ax1 = subplot(1,2,1); % Left subplot
gscatter(ax1,Acceleration,MPG,Model_Year,color(1:3,:))
title(ax1,'Left Subplot (Model Year)')
ax2 = subplot(1,2,2); % Right subplot
gscatter(ax2,Acceleration,MPG,Cylinders,color(4:6,:))
title(ax2,'Right Subplot (Cylinders)')

x-axis values, specified as a numeric vector. x must
have the same size as y.

Data Types: single | double

y — y-axis values numeric vector

y-axis values, specified as a numeric vector. y must
have the same size as x.

Data Types: single | double

g — Grouping variable categorical vector | logical vector | numeric vector | character array | string array | cell array of character vectors | cell array

Grouping variable, specified as a categorical vector, logical vector,
numeric vector, character array, string array, or cell array of character
vectors. Alternatively, g can be a cell array
containing several grouping variables (such as {g1 g2
g3}), in which case observations are in the same group if they
have common values of all grouping variables. Points in the same group
appear on the scatter plot with the same marker color, symbol, and
size.

The number of rows in g must be equal to the length
of x.

Example: species

Example: {Cylinders,Origin}

Data Types: categorical | logical | single | double | char | string | cell

clr — Marker colors character vector or string scalar of colors | matrix of RGB triplet values

Marker colors, specified as either a character vector or string scalar of
colors recognized by the plot function or a matrix of
RGB triplet values. Each RGB triplet is a three-element row vector whose
elements specify the intensities of the red, green, and blue components of
the color, respectively. Each intensity must be in the range [0,1].

This table lists the available color characters and their equivalent RGB
triplet values.

Long Name

Short Name

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

If you do not specify enough values for all groups, then
gscatter cycles through the specified values as
needed.

Example: 'rgb'

Example: [0 0 1; 0 0 0]

Data Types: char | string | single | double

sym — Marker symbols '.' (default) | character vector or string scalar of symbols

Marker symbols, specified as a character vector or string scalar of
symbols recognized by the plot function. This table
lists the available marker symbols.

Value

Description

'o'

Circle

'+'

Plus sign

'*'

Asterisk

'.'

Point

'x'

Cross

's'

Square

'd'

Diamond

'^'

Upward-pointing triangle

'v'

Downward-pointing triangle

'>'

Right-pointing triangle

'<'

Left-pointing triangle

'p'

Five-pointed star (pentagram)

'h'

Six-pointed star (hexagram)

'none'

No markers

If you do not specify enough values for all groups, then
gscatter cycles through the specified values as
needed.

Example: 'o+*v'

Data Types: char | string

siz — Marker sizes positive numeric vector

Marker sizes, specified as a positive numeric vector in points. The
default value is determined by the number of observations. If you do not
specify enough values for all groups, then gscatter
cycles through the specified values as needed.

Example: [6 12]

Data Types: single | double

doleg — Option to include legend 'on' (default) | 'off'

Option to include a legend, specified as either 'on' or
'off'. By default, the legend is displayed on the
graph.

xnam — x-axis label x variable name (default) | character vector | string scalar

x-axis label, specified as a character vector or string scalar.

Data Types: char | string

ynam — y-axis label y variable name (default) | character vector | string scalar

y-axis label, specified as a character vector or string scalar.

Data Types: char | string

ax — Axes for plot Axes object | UIAxes object

Axes for the plot, specified as an Axes or
UIAxes object. If you do not specify
ax, then gscatter creates the
plot using the current axes. For more information on creating an axes
object, see axes and uiaxes.

Graphics handles, returned as an array of Line objects.
Each Line object corresponds to one of the groups in
g. You can use dot notation to query and set
properties of the line objects. For a list of Line object
properties, see Line Properties.

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.