Plotting Time Series

Plotting time series from a vector

Let's start by loading time series data from a csv file:

>> timeseries = readtable('timeseries.csv')

[timeseries:255x4 table]

>> timeseries(1:3)# 

% displaying 3 first rows

Date Amazon Google Apple 15/06/2015 22.42 22.88 21.5 12/06/2015 22.58 20.75 21.48 11/06/2015 22.98 20.86 21.19

In order to plot time series the x-axis coordinates must use mathlayer® date numbers. Setting xAxisFormat option to 'date' allows to plot time series by converting the date number into a date string displayed on the x-axis.

The date format can be specified through xAxisDateFormat option:

>> o = struct % 

initializing options struct

o.xAxisFormat = 'date'

% we want to plot time series

o.xAxisDateFormat = '%d-%b-%Y'

% specifying the date format here

o.xAxisRotate = 25 %

rotating x-axis labels for clarity

o.markerSize = 2 plot(datenum(timeseries.Date, 'dd/mm/yyyy'), timeseries.Amazon, o)

Plotting time series using table input

The following example shows how to plot multiple time series using a table object as input argument:

>> timeseries.Date = datenum(timeseries.Date,'dd/mm/yyyy')
o = struct 

% initializing options struct

o.xAxisFormat = 'date'

% we want to plot time series

o.xAxisDateFormat = '%d-%b-%Y'

% specifying the date format here

o.xAxisRotate = 25

% rotating x-axis labels for clarity

plot(timeseries, o)

Related functions

plot | datenum