Matlab三维散点图绘制函数scatter3

3.3
(4)

今天,带来Matlab中绘制三维散点图的函数scatter3的使用方法。本文,主要介绍scatter3函数在Matlab中的常见用法、语法说明、创建三维散点图、改变标记大小、改变标记颜色、填充标记、设置标记类型、设置标记属性、指定三维散点图的坐标区、使用句柄设置散点序列属性等方面的介绍。

三维散点图表示三个变量变化的大致趋势,据此可以选择合适的函数对数据点进行拟合。用三组数据构成多个坐标点,考察坐标点的分布,判断三变量之间是否存在某种关联或总结坐标点的分布模式。散点图将序列显示为一组点。值由点在图表中的位置表示。类别由图表中的不同标记表示。散点图通常用于比较跨类别的聚合数据。

Matlab三维散点图绘制函数scatter3

下面我们将开始非常详细的 Matlab scatter3 函数语法介绍,实例引用,结果展示。首先,我们给出 Matlab 中关于 scatter3 函数的帮助文本如下:

>> help scatter3
 scatter3 3-D Scatter plot.
    scatter3(X,Y,Z,S,C) displays colored circles at the locations
    specified by the vectors X,Y,Z (which must all be the same size).  The
    area of each marker is determined by the values in the vector S (in
    points^2) and the colors of each marker are based on the values in C.  S
    can be a scalar, in which case all the markers are drawn the same
    size, or a vector the same length as X,Y, and Z.
    
    When C is a vector the same length as X,Y, and Z, the values in C
    are linearly mapped to the colors in the current colormap.  
    When C is a LENGTH(X)-by-3 matrix, the values in C specify the
    colors of the markers as RGB values.  C can also be a color string.
 
    scatter3(X,Y,Z) draws the markers with the default size and color.
    scatter3(X,Y,Z,S) draws the markers with a single color.
    scatter3(...,M) uses the marker M instead of 'o'.
    scatter3(...,'filled') fills the markers.
 
    scatter3(AX,...) plots into AX instead of GCA.
 
    H = scatter3(...) returns handles to scatter objects created.
 
    Use PLOT3 for single color, single marker size 3-D scatter plots.
 
    Example
       [x,y,z] = sphere(16);
       X = [x(:)*.5 x(:)*.75 x(:)];
       Y = [y(:)*.5 y(:)*.75 y(:)];
       Z = [z(:)*.5 z(:)*.75 z(:)];
       S = repmat([1 .75 .5]*10,numel(x),1);
       C = repmat([1 2 3],numel(x),1);
       scatter3(X(:),Y(:),Z(:),S(:),C(:),'filled'), view(-60,60)

常见用法

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

语法说明

scatter3(X,Y,Z) 在向量 X、Y 和 Z 指定的位置显示圆圈。

scatter3(X,Y,Z,S) 使用 S 指定的大小绘制每个圆圈。要绘制大小相等的圆圈,请将 S 指定为标量。要绘制具有特定大小的每个圆,请将 S 指定为向量。

scatter3(X,Y,Z,S,C) 使用 C 指定的颜色绘制每个圆圈。

  • 如果 C 是 RGB 三元组,或者是包含颜色名称的字符向量或字符串,则使用指定的颜色绘制所有圆圈。
  • 如果 C 是一个三列矩阵,其中 C 中的行数等于 X、Y 和 Z 的长度,则 C 的每行指定相应圆圈的 RGB 颜色值。
  • 如果 C 是长度与 X、Y 和 Z 的长度相同的向量,则 C 中的值线性映射到当前颜色图中的颜色。

scatter3(___,’filled’) 使用前面的语法中的任何输入参数组合填充这些圆。

scatter3(___,markertype) 指定标记类型。

scatter3(___,Name,Value) 使用一个或多个名称-值对组参数修改散点图。

scatter3(ax,___) 将图形绘制到 ax 指定的坐标区中,而不是当前坐标区 (gca) 中。选项 ax 可以位于前面的语法中的任何输入参数组合之前。

h = scatter3(___) 返回 Scatter 对象。在创建散点图后,可使用 h 修改其属性。

创建三维散点图

创建三维散点图。使用 sphere 定义向量 x、y 和 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)
Matlab三维散点图绘制函数scatter3

改变标记大小

使用 sphere 定义向量 x、y 和 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(:)];

定义向量 s 可指定标记大小。

S = repmat([100,50,5],numel(X),1);
s = S(:);

创建一个三维散点图并使用 view 更改图窗中坐标区的角度。

figure
scatter3(x,y,z,s)
view(40,35)
Matlab三维散点图绘制函数scatter3

x、y、z 和 s 中的相应项确定每个标记的位置和大小。

改变标记颜色

使用 sphere 定义向量 x、y 和 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(:)];

定义向量 s 和 c 以指定每个标记的大小和颜色。

S = repmat([50,25,10],numel(X),1);
C = repmat([1,2,3],numel(X),1);
s = S(:);
c = C(:);

创建一个三维散点图并使用 view 更改图窗中坐标区的角度。

figure
scatter3(x,y,z,s,c)
view(40,35)
Matlab三维散点图绘制函数scatter3

x、y、z 和 c 中的相应项确定每个标记的位置和颜色。

填充标记

将向量 x 和 y 创建为带随机干扰的余弦和正弦值。

z = linspace(0,4*pi,250);
x = 2*cos(z) + rand(1,250);
y = 2*sin(z) + rand(1,250);

创建一个三维散点图并填充标记。使用 view 可更改图窗中坐标区的角度。

scatter3(x,y,z,'filled')
view(-30,10)
Matlab三维散点图绘制函数scatter3

设置标记类型

初始化随机数生成器以使 rand 的输出可重复。将向量 x 和 y 定义为带随机干扰的余弦和正弦值。

rng default
z = linspace(0,4*pi,250);
x = 2*cos(z) + rand(1,250);
y = 2*sin(z) + rand(1,250);

创建一个三维散点图并设置标记类型。使用 view 可更改图窗中坐标区的角度。

figure
scatter3(x,y,z,'*')
view(-30,10)
Matlab三维散点图绘制函数scatter3

设置标记属性

初始化随机数生成器以使 rand 的输出可重复。将向量 x 和 y 定义为带随机干扰的余弦和正弦值。

rng default
z = linspace(0,4*pi,250);
x = 2*cos(z) + rand(1,250);
y = 2*sin(z) + rand(1,250);

创建一个三维散点图并设置标记边颜色和标记面颜色。使用 view 可更改图窗中坐标区的角度。

figure
scatter3(x,y,z,...
        'MarkerEdgeColor','k',...
        'MarkerFaceColor',[0 .75 .75])
view(-30,10)
Matlab三维散点图绘制函数scatter3

指定三维散点图的坐标区

从 R2019b 开始,您可以使用 tiledlayout 和 nexttile 函数显示平铺绘图。

加载 seamount 数据集以获取向量 x、y 和 z。调用 tiledlayout 函数以创建一个 2×1 平铺图布局。调用 nexttile 函数以创建坐标区对象 ax1 和 ax2。然后通过将坐标区对象指定为 scatter3 的第一个参数,在坐标区中创建单独的散点图。

load seamount
tiledlayout(2,1)
ax1 = nexttile;
ax2 = nexttile;
scatter3(ax1,x,y,z,'MarkerFaceColor',[0 .75 .75])
scatter3(ax2,x,y,z,'*')

由于我用的是Matlab2016,不支持tiledlayout和nexttile函数,这里用subplot函数来代替,代码如下:

load seamount
subplot(2,1,1)
scatter3(x,y,z,'MarkerFaceColor',[0 .75 .75])
subplot(2,1,2)
scatter3(x,y,z,'*')
Matlab三维散点图绘制函数scatter3

使用句柄设置散点序列属性

使用 sphere 函数创建向量 x、y 和 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(:)];

创建向量 s 和 c 以指定每个标记的大小和颜色。

S = repmat([70,50,20],numel(X),1);
C = repmat([1,2,3],numel(X),1);
s = S(:);
c = C(:);

创建一个三维散点图并返回散点序列对象。

h = scatter3(x,y,z,s,c);
Matlab三维散点图绘制函数scatter3

使用 RGB 三元组颜色值设置标记面颜色。使用圆点表示法设置属性。

h.MarkerFaceColor = [0 0.5 0.5];
Matlab三维散点图绘制函数scatter3

共计4人评分,平均3.3

到目前为止还没有投票~

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

文章目录

转载文章,原文出处:MathWorks官网,由古哥整理发布

如若转载,请注明出处:https://iymark.com/articles/768.html

(1)
微信公众号
古哥的头像古哥管理团队
上一篇 2020年11月09日 21:53
下一篇 2020年11月11日 21:34

你可能感兴趣的文章

发表回复

登录后才能评论
微信小程序
微信公众号