Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

animatedline

创建动画线条

说明

an = animatedline 创建一根没有任何数据的动画线条并将其添加到当前坐标区中。通过使用 addpoints 函数循环向线条中添加点来创建动画。

示例

an = animatedline(x,y) 创建一根包含由 xy 定义的初始数据点的动画线条。

an = animatedline(x,y,z) 创建一根包含由 xyz 定义的初始数据点的动画线条。

示例

an = animatedline(___,Name,Value) 使用一个或多个名称-值对组参量指定动画线条属性。例如,'Color','r' 将线条颜色设置为红色。在上述语法中的任何输入参量组合后使用此选项。

an = animatedline(ax,___) 将在由 ax 指定的坐标区中,而不是在当前坐标区中创建线条。请在前面任何语法中的所有其他输入参量之前指定 ax

示例

全部折叠

创建初始动画线条对象。然后,通过循环向线条中添加 1,000 个点。在添加每个新点后,使用 drawnow 在屏幕上显示该新点。

h = animatedline;
axis([0,4*pi,-1,1])

x = linspace(0,4*pi,1000);
y = sin(x);
for k = 1:length(x)
    addpoints(h,x(k),y(k));
    drawnow
end

要加快渲染速度,可在每次遍历循环时向线条中添加多个点或使用 drawnow limitrate

查询线条中的点。

[xdata,ydata] = getpoints(h);

清除线条中的点。

clearpoints(h)
drawnow

将动画线条的颜色设置为红色并将其线宽设置为 3 磅。

x = [1 2];
y = [1 2];
h = animatedline(x,y,'Color','r','LineWidth',3);

Figure contains an axes object. The axes object contains an object of type animatedline.

要绘制非数值点,如日期时间和持续时间值,请先用要绘制的类型的值初始化动画线条。您可以指定绘图中的第一个点或占位符值,如 NaTNaN

例如,在 x 轴上绘制日期时间值,在 y 轴上绘制持续时间值(分钟)。用 NaT 值和 minutes(NaN) 值初始化动画线条。然后创建一个日期时间向量 (x) 和持续时间向量 (y),并将这些向量中的点添加到动画线条中。

an = animatedline(NaT,minutes(NaN),"Marker","o");
x = datetime(2018,5,1:5);
y = minutes([1 7 3 11 4]);
addpoints(an,x,y)

将动画线条中的点数限制为 100 个。通过循环一次向线条中添加一个点。当线条包含 100 个点时,向线条添加新点会删除最旧的点。

h = animatedline('MaximumNumPoints',100);
axis([0,4*pi,-1,1])

x = linspace(0,4*pi,1000);
y = sin(x);
for k = 1:length(x)
    addpoints(h,x(k),y(k));
    drawnow
end

通过循环向动画线条中添加 100,000 个点。由于点的数目很大,因此每次通过循环向线条中添加一个点可能很慢。改为每次通过循环向线条中添加 100 个点以产生更快的动画。

h = animatedline;
axis([0,4*pi,-1,1])

numpoints = 100000;
x = linspace(0,4*pi,numpoints);
y = sin(x);
for k = 1:100:numpoints-99
    xvec = x(k:k+99);
    yvec = y(k:k+99);
    addpoints(h,xvec,yvec)
    drawnow
end

另一种用于创建更快动画的技术是使用 drawnow limitrate 代替 drawnow

通过循环向动画线条中添加 100,000 个点。由于点的数目很大,因此通过 drawnow 显示更改可能很慢。改用 drawnow limitrate 可以产生更快的动画。

h = animatedline;
axis([0,4*pi,-1,1])

numpoints = 100000;
x = linspace(0,4*pi,numpoints);
y = sin(x);
for k = 1:numpoints
    addpoints(h,x(k),y(k))
    drawnow limitrate
end

在屏幕上绘制更新之前先运行动画循环的多个迭代。在 drawnow 太慢或 drawnow limitrate 太快时可以使用此技术。

例如,每 1/30 秒更新一次屏幕。使用 tictoc 命令可跟踪屏幕更新间经过的时间。

h = animatedline;
axis([0,4*pi,-1,1])
numpoints = 10000;
x = linspace(0,4*pi,numpoints);
y = sin(x);
a = tic; % start timer
for k = 1:numpoints
    addpoints(h,x(k),y(k))
    b = toc(a); % check timer
    if b > (1/30)
        drawnow % update screen every 1/30 seconds
        a = tic; % reset timer after updating
    end
end
drawnow % draw final frame

更小的时间间隔会使屏幕更新更频繁,从而产生更慢的动画。例如,使用 b > (1/1000) 可以减慢动画速度。

输入参数

全部折叠

起始 x 坐标,指定为与 y 大小相同的标量或向量。

在极坐标中,x 对应于起始 theta 值。在地理坐标中,x 对应于以度为单位的起始纬度。

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

起始 y 坐标,指定为与 x 大小相同的标量或向量。

在极坐标中,y 对应于起始半径值。在地理坐标中,y 对应于以度为单位的起始经度。

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

起始 z 坐标,指定为标量或向量。

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

目标坐标区,指定为任何类型的坐标区、Group 对象或 Transform 对象。如果不指定此参量,则 animatedline 会使用当前坐标区。

名称-值参数

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

示例: animatedline(x,y,Color="red",Marker="o") 创建一个具有红色圆形标记的动画线条。

在 R2021a 之前: 使用逗号分隔每个名称和值,并用引号将 Name 引起来。例如,animatedline(x,y,"Color","red","Marker","o") 创建一个具有红色圆形标记的动画线条。

此处所列的动画线条属性只是一个子集。有关完整列表,请参阅 AnimatedLine 属性

线条颜色,指定为 RGB 三元组、十六进制颜色代码、颜色名称或短名称。默认值 [0 0 0] 对应于黑色。

对于自定义颜色,请指定 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"不适用不适用不适用无颜色

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

RGB 三元组十六进制颜色代码外观
[0 0.4470 0.7410]"#0072BD"

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

[0.8500 0.3250 0.0980]"#D95319"

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

[0.9290 0.6940 0.1250]"#EDB120"

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

[0.4940 0.1840 0.5560]"#7E2F8E"

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

[0.4660 0.6740 0.1880]"#77AC30"

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

[0.3010 0.7450 0.9330]"#4DBEEE"

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

[0.6350 0.0780 0.1840]"#A2142F"

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

线型,指定为下表中列出的选项之一。

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

Sample of solid line

"--"虚线

Sample of dashed line

":"点线

Sample of dotted line

"-."点划线

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

"none"无线条无线条

线宽,指定为以磅为单位的正值,其中 1 磅 = 1/72 英寸。如果该线条具有标记,则线条宽度也会影响标记边。

线宽不能小于像素的宽度。如果将线宽设置为小于系统上像素宽度的值,则线条显示为一个像素的宽度。

标记符号,指定为下表中列出的值之一。默认情况下,对象不显示标记。指定标记符号可在每个数据点或顶点添加标记。

标记描述生成的标记
"o"圆圈

Sample of circle marker

"+"加号

Sample of plus sign marker

"*"星号

Sample of asterisk marker

"."

Sample of point marker

"x"叉号

Sample of cross marker

"_"水平线条

Sample of horizontal line marker

"|"垂直线条

Sample of vertical line marker

"square"方形

Sample of square marker

"diamond"菱形

Sample of diamond marker

"^"上三角

Sample of upward-pointing triangle marker

"v"下三角

Sample of downward-pointing triangle marker

">"右三角

Sample of right-pointing triangle marker

"<"左三角

Sample of left-pointing triangle marker

"pentagram"五角形

Sample of pentagram marker

"hexagram"六角形

Sample of hexagram marker

"none"无标记不适用

标记大小,指定为以磅为单位的正值,其中 1 磅 = 1/72 英寸。

标记轮廓颜色,指定为 "auto"、RGB 三元组、十六进制颜色代码、颜色名称或短名称。默认值 "auto" 使用与 Color 属性相同的颜色。

对于自定义颜色,请指定 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"不适用不适用不适用无颜色

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

RGB 三元组十六进制颜色代码外观
[0 0.4470 0.7410]"#0072BD"

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

[0.8500 0.3250 0.0980]"#D95319"

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

[0.9290 0.6940 0.1250]"#EDB120"

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

[0.4940 0.1840 0.5560]"#7E2F8E"

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

[0.4660 0.6740 0.1880]"#77AC30"

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

[0.3010 0.7450 0.9330]"#4DBEEE"

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

[0.6350 0.0780 0.1840]"#A2142F"

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

标记填充颜色,指定为 "auto"、RGB 三元组、十六进制颜色代码、颜色名称或短名称。"auto" 选项使用与父坐标区的 Color 属性相同的颜色。如果您指定 "auto",并且坐标区图框不可见,则标记填充颜色为图窗的颜色。

对于自定义颜色,请指定 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"不适用不适用不适用无颜色

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

RGB 三元组十六进制颜色代码外观
[0 0.4470 0.7410]"#0072BD"

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

[0.8500 0.3250 0.0980]"#D95319"

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

[0.9290 0.6940 0.1250]"#EDB120"

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

[0.4940 0.1840 0.5560]"#7E2F8E"

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

[0.4660 0.6740 0.1880]"#77AC30"

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

[0.3010 0.7450 0.9330]"#4DBEEE"

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

[0.6350 0.0780 0.1840]"#A2142F"

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

存储并显示为线条的一部分的点的最大数目,指定为正值或 Inf。默认情况下,该值是一百万个点。如果点数超过允许的最大值,则动画线条会保留最近添加的点并丢弃该线条起始处的点。这些放弃的点不再显示在屏幕上,并且在使用 getpoints 时不会返回这些点。

使用该属性可限制在任何给定时间显示在屏幕上的点数,或限制使用的内存量。如果将值指定为 Inf,则动态线条不放弃任何点,但存储的点数受可用内存量的限制。

示例: 10

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

输出参量

全部折叠

AnimatedLine 对象。可在创建 AnimatedLine 对象后使用 an 修改对象,例如更改属性值或向线条上添加点。有关属性列表,请参阅 AnimatedLine 属性

局限性

动画线条不支持数据提示。

扩展功能

版本历史记录

在 R2014b 中推出

全部展开