Main Content

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

plot

绘制 timeseries

说明

示例

plot(ts) 绘制 ts 中的 timeseries 数据对时间的图,从而在样本之间进行插值。

示例

plot(ts,LineSpec) 使用线图绘制 timeseries 数据并对线条、标记或这两者应用指定的 specs

您也可以指定名称-值参数来定义 Line 属性

示例

全部折叠

创建一个时间序列对象,设置开始日期,然后绘制相对于开始日期的时间向量。

x = [2 5 8 2 11 3 6];
ts1 = timeseries(x,1:7);

ts1.Name = 'Daily Count';
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '01-Jan-2011';     % Set start date.
ts1.TimeInfo.Format = 'mmm dd, yy';       % Set format for display on x-axis.

ts1.Time = ts1.Time - ts1.Time(1);        % Express time relative to the start date.

plot(ts1)

Figure contains an axes object. The axes object with title Time Series Plot:Daily Count, ylabel Daily Count contains an object of type line.

基于流量计数数据创建两个时间序列对象,然后在相同坐标区上依次绘制它们。为一个时间序列添加一个事件,系统自动为事件显示一个红色标记。

load count.dat;
count1 = timeseries(count(:,1),1:24);
count1.Name = 'Oak St. Traffic Count';
count1.TimeInfo.Units = 'hours';
plot(count1,':b')
grid on

Figure contains an axes object. The axes object with title Time Series Plot:Oak St. Traffic Count, xlabel Time (hours), ylabel Oak St. Traffic Count contains an object of type line.

获取最大值的时间并将其添加为事件:

[~,index] = max(count1.Data);
max_event = tsdata.event('peak',count1.Time(index));
max_event.Units = 'hours';

将事件添加到时间序列中:

count1 = addevent(count1,max_event);

将绘图替换为显示该事件的新绘图:

plot(count1,'.-b')
grid on

Figure contains an axes object. The axes object with title Time Series Plot:Oak St. Traffic Count, xlabel Time (hours), ylabel Oak St. Traffic Count contains 2 objects of type line. One or more of the lines displays its values using only markers

根据同一数据源的第 2 列生成新的时间序列对象:

count2 = timeseries(count(:,2),1:24);
count2.Name = 'Maple St. Traffic Count';
count2.TimeInfo.Units = 'Hours';

打开 hold 以向绘图添加新数据:

hold on

绘图方法不向保留的绘图添加标签。使用属性/值对组自定义标记:

plot(count2,'s-m','MarkerSize',6),

Figure contains an axes object. The axes object with title Time Series Plot:Oak St. Traffic Count, xlabel Time (hours), ylabel Oak St. Traffic Count contains 3 objects of type line. One or more of the lines displays its values using only markers

清除了标签,因此手动生成它们:

title('Time Series: Oak Street and Maple Street')
xlabel('Hour of day')
ylabel('Vehicle count')

在左上方添加图例:

legend('Oak St.','Maple St.','Location','northwest')

Figure contains an axes object. The axes object with title Time Series: Oak Street and Maple Street, xlabel Hour of day, ylabel Vehicle count contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Oak St., Maple St..

输入参数

全部折叠

输入 timeseries,指定为标量。

线型、标记和颜色,指定为包含符号的字符串标量或字符向量。符号可以按任意顺序显示。您不需要同时指定所有三个特征(线型、标记和颜色)。例如,如果忽略线型,只指定标记,则绘图只显示标记,不显示线条。

示例: "--or" 是带有圆形标记的红色虚线。

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

Sample of solid line

"--"虚线

Sample of dashed line

":"点线

Sample of dotted line

"-."点划线

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

标记描述生成的标记
"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

颜色名称短名称RGB 三元组外观
"red""r"[1 0 0]

Sample of the color red

"green""g"[0 1 0]

Sample of the color green

"blue""b"[0 0 1]

Sample of the color blue

"cyan" "c"[0 1 1]

Sample of the color cyan

"magenta""m"[1 0 1]

Sample of the color magenta

"yellow""y"[1 1 0]

Sample of the color yellow

"black""k"[0 0 0]

Sample of the color black

"white""w"[1 1 1]

Sample of the color white

提示

  • plot 函数自动生成标题和轴标签。这些标签是:

    • 绘图标题 - 'Time Series Plot: <name>'

      其中,<name> 是指定给 ts.Name 的字符串或默认为 'unnamed'

    • X 轴标签 - 'Time (<units>)'

      其中,<units>ts.TimeInfo.Units 字段的值,默认为 'seconds'

    • Y 轴标签 - '<name>'

      其中,<name> 是指定给 ts.Name 的字符串或默认为 'unnamed'

  • 可以在时间序列绘图上放置新的时间序列数据(例如,通过设置 hold on 并发出另一个 timeseries/plot 命令)。将数据添加到绘图中时,标题和轴标签变为空的字符串以避免标签混乱。可以在绘制后使用 titlexlabelylabel 命令添加自己的标签。

  • 定义时,时间序列事件在绘图中标记为用红色填充的圆形标记。除了数据定义的任何事件标记外,您还可以使用 linespec 或名称/值语法为所有数据点指定标记。事件标记绘制在您定义的标记的上方。

  • 赋予 ts.DataInfo.Interpolation.Name 的值控制在绘制时间序列数据并重采样时 plot 方法使用的插值类型。调用 timeseries 方法 setinterpmethod 以便将默认的线性插值更改为零阶保持插值(楼梯形)。该方法创建一个新的 timeseries 对象,如果需要,您可以用新对象覆盖原来的对象。例如,要使时间序列 ts 使用零阶保持插值,请键入以下内容:

    ts = ts.setinterpmethod('zoh');

版本历史记录

在 R2006a 之前推出