Main Content

plot

Plottimeseries

Description

example

plot(ts)plots thetimeseriesdata intsagainst time, interpolating values between samples.

example

plot(ts,LineSpec)plots thetimeseriesdata using a line graph and applies the specifiedspecsto lines, markers, or both.

You can also specify name-value arguments to defineLine Properties.

Examples

collapse all

Create a time series object, set the start date, and then plot the time vector relative to the start date.

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 contains an object of type line.

Create two time series objects from traffic count data, and then plot them in sequence on the same axes. Add an event to one series, which is automatically displayed with a red marker.

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

Figure contains an axes object. The axes object with title Time Series Plot:Oak St. Traffic Count contains an object of type line.

Obtain time of maximum value and add it as an event:

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

Add the event to the time series:

count1 = addevent(count1,max_event);

Replace plot with new one showing the event:

plot(count1,'.-b') gridon

Figure contains an axes object. The axes object with title Time Series Plot:Oak St. Traffic Count contains 2 objects of type line.

Make a new time series object from column 2 of the same data source:

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

Turn hold on to add the new data to the plot:

holdon

The plot method does not add labels to a held plot. Use property/value pairs to customize markers:

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

Figure contains an axes object. The axes object with title Time Series Plot:Oak St. Traffic Count contains 3 objects of type line.

Labels are erased, so generate them manually:

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

Add a legend in the upper left:

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

Figure contains an axes object. The axes object with title Time Series: Oak Street and Maple Street contains 3 objects of type line. These objects represent Oak St., Maple St..

Input Arguments

collapse all

Inputtimeseries, specified as a scalar.

Line style, marker, and color, specified as a character vector or string containing symbols. The symbols can appear in any order. You do not need to specify all three characteristics (line style, marker, and color). For example, if you omit the line style and specify the marker, then the plot shows only the marker and no line.

Example:'--or'是一个红色虚线圆圈标记吗

Line Style Description Resulting Line
“- - -” Solid line

Sample of solid line

'--' Dashed line

样本的虚线

':' Dotted line

Sample of dotted line

'-.' Dash-dotted line

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

Marker Description Resulting Marker
'o' Circle

Sample of circle marker

'+' Plus sign

Sample of plus sign marker

'*' Asterisk

Sample of asterisk marker

'.' 阿宝int

Sample of point marker

'x' Cross

Sample of cross marker

'_' Horizontal line

Sample of horizontal line marker

'|' Vertical line

Sample of vertical line marker

's' Square

Sample of square marker

'd' Diamond

Sample of diamond line marker

'^' Upward-pointing triangle

Sample of upward-pointing triangle marker

'v' Downward-pointing triangle

Sample of downward-pointing triangle marker

'>' Right-pointing triangle

Sample of right-pointing triangle marker

'<' Left-pointing triangle

Sample of left-pointing triangle marker

'p' Pentagram

Sample of pentagram marker

'h' Hexagram

Sample of hexagram marker

Color Name Short Name RGB Triplet Appearance
'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

Tips

  • Theplotfunction generates titles and axis labels automatically. These labels are:

    • Plot Title —'Time Series Plot: '

      whereis the string assigned tots.Name阿,r by default,'unnamed'

    • X-Axis Label —'Time ()'

      whereis the value of thets.TimeInfo.Unitsfield, which defaults to'seconds'

    • Y-Axis Label —''

      whereis the string assigned tots.Name阿,r by default,'unnamed'

  • You can place new time series data on a time series plot (by settinghold on, for example, and issuing anothertimeseries/plotcommand). When you add data to a plot, the title and axis labels become blank strings to avoid labeling confusion. You can add your own labels after plotting using thetitle,xlabel, andylabelcommands.

  • Time series events, when defined, are marked in the plot with a circular marker with red fill. You can also specify markers for all data points using alinespecor name/value syntax in addition to any event markers your data defines. The event markers plot on top of the markers you define.

  • The value assigned tots.DataInfo.Interpolation.Namecontrols the type of interpolation theplotmethod uses when plotting and resampling time series data. Invoke thetimeseriesmethodsetinterpmethodto change default linear interpolation to zero-order hold interpolation (staircase). This method creates a newtimeseriesobject, with which you can overwrite the original one if you want. For example, to cause time serieststo use zero-order hold interpolation, type the following:

    ts = ts.setinterpmethod('zoh');

Version History

Introduced before R2006a