主要内容

compassplot

具有从原点发射出的箭头的极坐标图

自 R2024b 起

  • Polar plot with arrows emanating from the center

说明

向量和矩阵数据

compassplot(theta,rho) 在极坐标中绘制源于原点的箭头(矢量)。theta 值以弧度为单位控制箭头方向。rho 值以半径值形式控制箭头的幅值。

  • 要绘制一组箭头,请将 thetarho 指定为等长向量。

  • 要绘制多组箭头,请将 thetarho 指定为矩阵,或将两者都指定为大小相同的矩阵。

示例

compassplot(Z) 绘制 Z 中的复数。将 Z 指定为包含 a+bi 形式的值的向量或矩阵。

示例

表数据

compassplot(tbl,thetavar,rhovar) 绘制表 thetavar 中的变量 rhovartbl。要绘制一组箭头,请为 thetavarrhovar 各指定一个变量。要绘制多组箭头,请为 thetavarrhovar 或两者指定多个变量。

示例

其他选项

compassplot(pax,___) 在目标极坐标区 pax 中显示罗盘图。将极坐标区指定为上述任一语法中的第一个参量。

示例

compassplot(___,Name=Value) 使用一个或多个名称-值参量指定 PolarCompassPlot 属性。在所有其他输入参量之后指定名称-值参量。有关属性列表,请参阅 PolarCompassPlot Properties

示例

cp = compassplot(___) 返回 PolarCompassPlot 对象。创建绘图后,使用 cp 获取和设置绘图的属性。有关属性列表,请参阅 PolarCompassPlot Properties

示例

示例

全部折叠

创建向量 thetarho,并在罗盘图中绘制其值。

rho = [1 3 2 2];
theta = [0 pi/4 3*pi/4 5*pi/4];
compassplot(theta,rho)

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type compassplot.

通过获取极坐标区并将 ThetaAxisUnits 属性设置为 "radians",将 theta 轴刻度标签的显示更改为显示弧度值。

pax = gca;
pax.ThetaAxisUnits = "radians";

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type compassplot.

要绘制多组箭头,请将至少一组坐标指定为矩阵。您也可以在绘图命令之间调用 hold 函数。

创建一个 4×2 风向值矩阵和一个 4×2 风速值矩阵。每个矩阵中的第一列对应于上午的数据,第二列对应于晚上的数据。将数据绘制为一个具有图例的罗盘图。

windDirection = deg2rad([0.5 185; 30 200; 45 230; 75 250]);
windSpeed = [20 15; 15 30; 12 25; 20 45];
compassplot(windDirection,windSpeed)
legend(["Morning Winds" "Evening Winds"])

Figure contains an axes object with type polaraxes. The polaraxes object contains 2 objects of type compassplot. These objects represent Morning Winds, Evening Winds.

您也可以一次绘制一组箭头,并在绘图命令之间调用 hold 函数。

% Create figure and data
figure
morningDirection = deg2rad([0.5 30 45 75]);
eveningDirection = deg2rad([185 200 230 250]);
morningSpeed = [20 15 12 20];
eveningSpeed = [15 30 25 45];

% Plot morning and evening data separately
compassplot(morningDirection,morningSpeed)
hold on
compassplot(eveningDirection,eveningSpeed)
hold off
legend(["Morning Winds" "Evening Winds"])

Figure contains an axes object with type polaraxes. The polaraxes object contains 2 objects of type compassplot. These objects represent Morning Winds, Evening Winds.

要绘制复数值,请指定一个由复数组成的向量或矩阵。例如,创建包含介于 -2 和 2 之间的数值的矩阵 ab。创建一个由 a+bi 形式的复数组成的矩阵 Z 并对其绘图。compassplot 使用不同颜色分别绘制 Z 的每列。

[a,b] = meshgrid(-2:2);
Z = a + b*1i;
compassplot(Z)

Figure contains an axes object with type polaraxes. The polaraxes object contains 5 objects of type compassplot.

您可以通过设置属性来修改绘图的各个方面。您可以在调用 compassplot 时通过指定名称-值参量来设置属性,也可以稍后使用圆点表示法设置属性。

例如,绘制七个值,并通过设置 LineWidth 名称-值参量来指定线条粗细。此外,还要指定输出参量以存储 PolarCompassPlot 对象。

theta = 0:pi/6:pi;
rho = [1 0.8 0.7 0.7 0.7 0.8 1];
cp = compassplot(theta,rho,LineWidth=1.5);

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type compassplot.

通过设置 PolarCompassPlot 对象 cp 的属性来进一步修改外观。将线条颜色更改为品红色,并将线型更改为虚线。

cp.Color = "magenta";
cp.LineStyle = "--";

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type compassplot.

创建一个包含两个变量 thetarho 的表。

theta = [0 pi/4 pi/2]';
rho = [1 3 2]';
t = table(theta,rho);

基于表创建一个罗盘图。

compassplot(t,"theta","rho")

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type compassplot.

要绘制到特定的极坐标区对象中,请在调用 compassplot 时将极坐标区指定为第一个参量。例如,创建一个极坐标区对象并绘制到其中。此策略在以下情况下很有用:

  • 您不希望 compassplot 创建一个新极坐标区对象。

  • 您要保留现有坐标区对象的内容。

  • 您正在 App 中创建绘图。

pax = polaraxes;
theta = [0 pi/4 pi/2];
rho = [1 2 3];
compassplot(pax,theta,rho)

Figure contains an axes object with type polaraxes. The polaraxes object contains an object of type compassplot.

输入参数

全部折叠

thetarho 值,指定为一对标量、一对向量、一个向量和一个矩阵或一对矩阵。指定 theta 值(以弧度为单位)。thetarho 的大小和形状取决于数据的形状。下表说明了最常见的情况。

绘图类型如何指定坐标
单个箭头

thetarho 指定为标量。例如:

compassplot(pi/2,1)

一组箭头

指定 thetarho 为相同长度的行向量或列向量的任意组合。例如:

theta = [0 pi/4 pi/2];
rho = [1; 2; 3];
compassplot(theta,rho)

具有不同颜色的多组箭头

如果所有数据集共享相同的 thetarho 值,请将共享坐标指定为一个向量,将其他坐标指定为一个矩阵。该向量的长度必须与该矩阵的维度之一相匹配。(如果您指定方阵,compassplot 会为矩阵的每列绘制一组箭头。)

例如,绘制分别包含三个箭头的两组箭头,这些箭头具有相同的 theta 值。

theta = [0 pi/4 pi/2];
rho = [4 5 6; 7 8 9];
compassplot(theta,rho)

或者,将 thetarho 指定为大小相等的矩阵。compassplot 使用矩阵中的列绘制各组箭头。例如,绘制三组各包含两个箭头的箭头图。

theta = [0 pi/6 pi/2; pi/8 pi/4 pi];
rho = [1 2 3; 4 5 6];
compassplot(theta,rho)

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

复数值,指定为由 a+bi 形式的值组成的标量、向量或矩阵。MATLAB® 使用 theta = angle(Z)rho = abs(Z)Z 转换为极坐标形式。

Z 的大小和形状取决于数据的形状。下表说明了最常见的情况。

绘图类型如何指定坐标
单个箭头

Z 指定为标量复数值。例如:

Z = 0+1i; 
compassplot(Z)

一组箭头

Z 指定为复数值向量。例如:

Z = [0+1i 1+2i 1+3i];
compassplot(Z)

具有不同颜色的多组箭头

Z 指定为复数值矩阵。compassplot 为矩阵中的每列绘制一组单独的箭头。例如,绘制三组分别包含两个箭头的箭头图:

Z = [0+1i 1+2i 1+3i; 0-1i 1-2i 1-3i];
compassplot(Z)

数据类型: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

包含要绘制的数据的源表,指定为表或时间表。

包含 thetarho 值的表变量,指定为一对表变量索引。

指定表索引

对于您指定的每个变量,您可以使用以下任何一种索引方案。

索引方案示例

变量名称:

  • 字符串数组、字符向量或元胞数组。

  • pattern 对象。

  • "A"'A' - 名为 A 的变量

  • ["A","B"]{'A','B'} - 两个名为 AB 的变量

  • "Var"+digitsPattern(1) - 变量名为 "Var" 后跟一个数字

变量索引:

  • 引用变量在表中位置的索引编号。

  • 由数字组成的向量。

  • 逻辑向量。通常,此向量的长度与变量的数目相同,但可以省略尾部的 0false 值。

  • 3 - 表中的第三个变量

  • [2 3] - 表中的第二个和第三个变量

  • [false false true] - 第三个变量

变量类型:

  • vartype 下标,用于选择指定类型的变量。

  • vartype("categorical") - 包含分类值的所有变量

绘制您的数据

您指定的表变量可以包含任何数值数据类型。

要绘制一个数据集,请为 thetavarrhovar 各指定一个变量。例如,创建一个包含四个变量的表。绘制 theta1rho1 变量。

% Create a table of theta and rho values
theta1 = [0 pi/4 pi/2 3*pi/4]';
theta2 = theta1 + pi;
rho1 =  [1 2 3 4]';
rho2 = [5 6 7 8]';
tbl = table(theta1,theta2,rho1,rho2);

% Create compass plot
compassplot(tbl,"theta1","rho1")

要一起绘制多个数据集,请为 thetavarrhovar 或两者指定多个变量。如果为 thetavarrhovar 都指定多个变量,则变量数目必须相同。例如,通过为 thetavarrhovar 各指定两个变量来创建一个罗盘图。

compassplot(tbl,["theta1" "theta2"],["rho1" "rho2"])

您还可以对表变量使用不同索引方案。例如,将 thetavar 指定为变量名称,将 rhovar 指定为索引编号。

compassplot(tbl,"theta1",3)

目标坐标区,指定为 PolarAxes 对象。如果未指定坐标区,则 compassplot 将绘制到当前坐标区中;或如果不存在 PolarAxes 对象,则创建一个对象。

名称-值参数

全部折叠

将可选参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。

示例: compassplot([0 pi/4 pi/2],[1 2 3],LineWidth=1.5) 使用 1.5 磅的线宽创建一个罗盘图。

注意

此处所列的属性只是一部分。有关完整列表,请参阅 PolarCompassPlot Properties

箭头颜色,指定为 RGB 三元组、十六进制颜色代码、颜色名称或短名称。

对于自定义颜色,请指定 RGB 三元组或十六进制颜色代码。

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

  • 十六进制颜色代码是字符串标量或字符向量,以井号 (#) 开头,后跟三个或六个十六进制数字,范围可以是 0F。这些值不区分大小写。因此,颜色代码 "#FF8800""#ff8800""#F80""#f80" 是等效的。

此外,还可以按名称指定一些常见的颜色。下表列出了一些命名颜色选项、其等效 RGB 三元组及十六进制颜色代码。

颜色名称短名称RGB 三元组十六进制颜色代码外观
"red""r"[1 0 0]"#FF0000"

Sample of the color red

"green""g"[0 1 0]"#00FF00"

Sample of the color green

"blue""b"[0 0 1]"#0000FF"

Sample of the color blue

"cyan" "c"[0 1 1]"#00FFFF"

Sample of the color cyan

"magenta""m"[1 0 1]"#FF00FF"

Sample of the color magenta

"yellow""y"[1 1 0]"#FFFF00"

Sample of the color yellow

"black""k"[0 0 0]"#000000"

Sample of the color black

"white""w"[1 1 1]"#FFFFFF"

Sample of the color white

"none"不适用不适用不适用无颜色

下表列出了浅色和深色主题中绘图的默认调色板。

调色板调色板颜色

"gem" - 浅色主题默认值

在 R2025a 之前的版本中: 大多数绘图默认使用这些颜色。

Sample of the "gem" color palette

"glow" - 深色主题默认值

Sample of the "glow" color palette

您可以使用 orderedcolorsrgb2hex 函数获取这些调色板的 RGB 三元组和十六进制颜色代码。例如,获取 "gem" 调色板的 RGB 三元组并将其转换为十六进制颜色代码。

RGB = orderedcolors("gem");
H = rgb2hex(RGB);

在 R2023b 之前的版本中: 使用 RGB = get(groot,"FactoryAxesColorOrder") 获取 RGB 三元组。

在 R2024a 之前的版本中: 使用 H = compose("#%02X%02X%02X",round(RGB*255)) 获取十六进制颜色代码。

箭杆的样式,指定为下表中列出的线型之一。

线型描述表示的线条
"-"实线

Sample of solid line

"--"虚线

Sample of dashed line

":"点线

Sample of dotted line

"-."点划线

Sample of dash-dotted line, with alternating dashes and dots

"none"无线条无线条

箭头线的宽度,指定为以磅为单位的正值。一磅等于 1/72 英寸。默认值为 0.5 磅。

版本历史记录

在 R2024b 中推出

另请参阅

属性