Main Content

gscatter

分组散点图

说明

示例

gscatter(x,y,g) 创建 xy 的散点图,按 g 分组。输入 xy 是大小相同的向量。

示例

gscatter(x,y,g,clr,sym,siz) 为每个组指定标记颜色 clr、符号 sym 和大小 siz

gscatter(x,y,g,clr,sym,siz,doleg) 控制是否在图上显示图例。默认情况下 gscatter 会创建图例。

示例

gscatter(x,y,g,clr,sym,siz,doleg,xnam,ynam) 指定用于 x 轴和 y 轴标签的名称。如果不提供 xnamynam,并且 xy 输入是带名称的变量,则 gscatter 用变量名称对坐标区加标签。

示例

gscatter(ax,___) 使用由坐标区对象 ax 指定的绘图坐标区。将 ax 指定为第一个输入参数,后跟先前语法中的任意输入参数组合。

示例

h = gscatter(___) 返回与 g 中的组对应的图形句柄。

您可以为 clrsymsiz 传入 [],以使用其默认值。

示例

全部折叠

加载 carsmall 数据集。

load carsmall

在 x 轴上绘制 Displacement 值,在 y 轴上绘制 Horsepower 值。gscatter 使用变量名称作为坐标轴的默认标签。按 Model_Year 对数据点分组。

gscatter(Displacement,Horsepower,Model_Year)

Figure contains an axes object. The axes object with xlabel Displacement, ylabel Horsepower contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent 70, 76, 82.

加载 discrim 数据集。

load discrim

该数据集包含基于气候、住房、教育和健康等九个方面的城市评分。矩阵 ratings 包含评分信息。

绘制矩阵 group 中按城市规模分组的气候评分(第一列)和住房评分(第二列)之间的关系。为每个组选择不同颜色和绘图符号。

gscatter(ratings(:,1),ratings(:,2),group,'br','xo')
xlabel('climate')
ylabel('housing')

Figure contains an axes object. The axes object with xlabel climate, ylabel housing contains 2 objects of type line. One or more of the lines displays its values using only markers These objects represent 1, 2.

加载 hospital 数据集。

load hospital

绘制住院患者的年龄和体重。根据患者的性别和吸烟状况对其进行分组。用 o 符号表示非吸烟者,用 * 符号表示吸烟者。

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

Figure contains an axes object. The axes object with xlabel Age, ylabel Weight contains 4 objects of type line. One or more of the lines displays its values using only markers These objects represent Female,0, Female,1, Male,0, Male,1.

加载 carsmall 数据集。创建一个包含两个子图的图窗,并以 ax1ax2 形式返回 axes 对象。通过引用对应的 Axes 对象,在每个坐标区中创建一个散点图。在左侧子图中,使用 Model_Year 变量对数据进行分组。在右侧子图中,使用 Cylinders 变量对数据进行分组。通过将对应的 Axes 对象传递给 title 函数,为每个绘图添加标题。

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

Figure contains 2 axes objects. Axes object 1 with title Left Subplot (Model Year), xlabel Acceleration, ylabel MPG contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent 70, 76, 82. Axes object 2 with title Right Subplot (Cylinders), xlabel Acceleration, ylabel MPG contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent 4, 6, 8.

使用由 hsv 函数确定的颜色图指定标记颜色。

加载激光雷达扫描数据集,该数据集包含一辆车周围对象的坐标,存储为三维点的集合。

load('lidar_subset.mat') 
loc = lidar_subset;

要突出显示该车辆周围的环境,请将感兴趣区域设置为车辆左右各 20 米、车辆前后各 20 米以及路面上方的区域。

xBound = 20; % in meters
yBound = 20; % in meters
zLowerBound = 0; % in meters

裁剪数据以仅包含指定区域内的点。

indices = loc(:,1) <= xBound & loc(:,1) >= -xBound ...
    & loc(:,2) <= yBound & loc(:,2) >= -yBound ...
    & loc(:,3) > zLowerBound;
loc = loc(indices,:);

使用具有两两距离的 dbscan 对数据进行聚类。

D = pdist2(loc,loc);
idx = dbscan(D,2,50,'Distance','precomputed');

使用 gscatter 函数将生成的簇可视化为二维组散点图。默认情况下,gscatter 使用七种 MATLAB 默认颜色。如果唯一簇的数量超过七个,该函数将根据需要循环使用这些默认颜色。找出簇数,并使用 hsv 函数生成相应的颜色数。指定标记颜色以对每个簇使用唯一颜色。

numGroups = length(unique(idx));
clr = hsv(numGroups);
gscatter(loc(:,1),loc(:,2),idx,clr)
xlabel('x')
ylabel('y')

Figure contains an axes object. The axes object with xlabel x, ylabel y contains 12 objects of type line. One or more of the lines displays its values using only markers These objects represent -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11.

加载 carbig 数据集。

load carbig

创建一个散点图,比较 AccelerationMPG。根据 Origin 对数据点进行分组。

h = gscatter(Acceleration,MPG,Origin)
h = 
  7x1 Line array:

  Line    (USA)
  Line    (France)
  Line    (Japan)
  Line    (Germany)
  Line    (Sweden)
  Line    (Italy)
  Line    (England)

显示与标注为 (Japan) 的组对应的 Line 对象。

jgroup = h(3)
jgroup = 
  Line (Japan) with properties:

              Color: [0.9290 0.6940 0.1250]
          LineStyle: 'none'
          LineWidth: 0.5000
             Marker: '.'
         MarkerSize: 15
    MarkerFaceColor: 'none'
              XData: [15 14.5000 14.5000 14 19 18 15.5000 13.5000 17 14.5000 16.5000 19 16.5000 13.5000 13.5000 19 21 16.5000 19 15 15.5000 16 13.5000 17 17.5000 17.4000 17 16.4000 15.5000 18.5000 16.8000 18.2000 16.4000 14.5000 ... ] (1x79 double)
              YData: [24 27 27 25 31 35 24 19 28 23 27 20 22 18 20 31 32 31 32 24 26 29 24 24 33 33 32 28 19 31.5000 33.5000 26 30 22 21.5000 32.8000 39.4000 36.1000 27.5000 27.2000 21.1000 23.9000 29.5000 34.1000 31.8000 38.1000 ... ] (1x79 double)

  Use GET to show all properties

Japan 组的标记颜色更改为黑色。

jgroup.Color = 'k';

Figure contains an axes object. The axes object with xlabel Acceleration, ylabel MPG contains 7 objects of type line. One or more of the lines displays its values using only markers These objects represent USA, France, Japan, Germany, Sweden, Italy, England.

输入参数

全部折叠

x 轴值,指定为数值向量。x 必须与 y 具有相同的大小。

数据类型: single | double

y 轴值,指定为数值向量。y 必须与 x 具有相同的大小。

数据类型: single | double

分组变量,指定为分类向量、逻辑向量、数值向量、字符数组、字符串数组或字符向量元胞数组。g 也可以是包含若干分组变量的元胞数组(如 {g1 g2 g3}),在这种情况下,所有分组变量的值相同的观测值都会放在同一个组中。同一组中的点以相同的标记颜色、符号和大小显示在散点图上。

g 中的行数必须等于 x 的长度。

示例: species

示例: {Cylinders,Origin}

数据类型: categorical | logical | single | double | char | string | cell

标记颜色,指定为由短颜色名称组成的字符向量或字符串标量或 RGB 三元组矩阵。

如果想自定义颜色,请指定一个 RGB 三元组矩阵。RGB 三元组是包含三个元素的行向量,其元素分别指定颜色中红、绿、蓝分量的强度。强度值必须位于 [0,1] 范围内,例如 [0.4 0.6 0.7]

此外,还可以按名称指定一些常见的颜色。下表列出了命名颜色选项和等效的 RGB 三元组。

短名称RGB 三元组外观
'r'[1 0 0]

Sample of the color red

'g'[0 1 0]

Sample of the color green

'b'[0 0 1]

Sample of the color blue

'c'[0 1 1]

Sample of the color cyan

'm'[1 0 1]

Sample of the color magenta

'y'[1 1 0]

Sample of the color yellow

'k'[0 0 0]

Sample of the color black

'w'[1 1 1]

Sample of the color white

以下是 MATLAB 在许多类型的绘图中使用的默认颜色的 RGB 三元组颜色代码。

RGB 三元组外观
[0 0.4470 0.7410]

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

[0.8500 0.3250 0.0980]

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

[0.9290 0.6940 0.1250]

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

[0.4940 0.1840 0.5560]

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

[0.4660 0.6740 0.1880]

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

[0.3010 0.7450 0.9330]

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

[0.6350 0.0780 0.1840]

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

clr 的默认值为包含 MATLAB 默认颜色的 RGB 三元组矩阵。

如果您没有为 g 中的所有唯一组指定足够多的颜色,则 gscatter 会循环使用 clr 中指定的值。如果在唯一组的数量超过默认颜色数量 (7) 时使用默认值,则 gscatter 会根据需要循环使用这些默认值。

示例: 'rgb'

示例: [0 0 1; 0 0 0]

数据类型: char | string | single | double

标记符号,指定为由 plot 函数识别的符号组成的字符向量或字符串标量。下表列出了可用的标记符号。

描述
'o'圆形
'+'加号
'*'星号
'.'
'x'叉号
's'正方形
'd'菱形
'^'上三角
'v'下三角
'>'右三角
'<'左三角
'p'五角星(五角形)
'h'六角星(六角形)
'none'无标记

如果没有为所有组指定足够多的值,则 gscatter 会根据需要循环使用指定的值。

示例: 'o+*v'

数据类型: char | string

标记大小,指定为以磅为单位的正数值向量。默认值由观测值的数目确定。如果没有为所有组指定足够多的值,则 gscatter 会根据需要循环使用指定的值。

示例: [6 12]

数据类型: single | double

包含图例的选项,指定为 'on''off'。默认情况下,图例显示在图上。

x 轴标签,指定为字符向量或字符串标量。

数据类型: char | string

y 轴标签,指定为字符向量或字符串标量。

数据类型: char | string

绘图的坐标区,指定为 AxesUIAxes 对象。如果未指定 ax,则 gscatter 使用当前坐标区创建绘图。有关创建坐标区对象的详细信息,请参阅 axesuiaxes

输出参数

全部折叠

图形句柄,以 Line 对象数组形式返回。每个 Line 对象对应于 g 中的组之一。您可以使用圆点表示法来查询和设置线条对象的属性。有关 Line 对象属性的列表,请参阅 Line 属性

版本历史记录

在 R2006a 之前推出

全部展开