Showing time on the axis of a plot in Matlab


I use Matlab quite a lot in my work, and part of that consists of plotting time series data. If you have a sequence of time intervals, let’s say representing seconds and you want to show these in time format rather than a sequence of numbers, this post might help you. Matlab 2015b is used.

We have a dataset called d of 200 points, where each point represents a 1 second duration. The measurements are binary values of 0 and 1. Now let’s plot the time series with time on x axis, and the measurements on the y axis.

Note: The time series duration vector ts is a sequence of numbers showing the durations over which measurements are taken, sort of indices. Since the data is collected over a duration, there is no measurement at time zero. The first event occurs after a duration of 1 second. To make the plot look nicer I have started the time from 0, but when plotting the first point in dataset d, the corresponding time instance will be 1 (the 2nd ts point).

d = [1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1];

%% Regular Figure
hFig = figure;
hAx = gca;
ts = 0:length(d); 
stairs(ts(2:end), d, 'LineWidth', 2)
hAx.FontSize = 24;

hAx.YLim = [min(d)-0.5, max(d)+0.5];
hAx.YTick = [0,1];
hAx.XLabel.String = 'Time (Seconds)';


The y axis shows the measurement during an interval. The x axis shows the time increments. Since, in this case the time intervals were 1 second, it is represented by 0:length(d). For small data showing the time in number format could be OK, but if you have larger data, it wouldn’t look very nice. Thus, let’s convert the x axis so that it will show time in minute:second format.

%% Figure with time on the x axis
% Arrange the x axis to show duration
t = datetime(ts, 'ConvertFrom', 'epochtime', 'Epoch', 0, 'Format', 'mm:ss');

hAx.XTick = linspace(min(ts), max(ts), 10);
hAx.XTickLabel = char(linspace(t(1), t(end), 10));
hAx.FontSize = 24;
hAx.XLabel.String = 'Time (MM:SS)';


Now we can see the time on the x axis. The datetime function converts the input timestamp into date-time format based on the Format provided.

Since I partitioned the data into 9 equal spaces with 10 points, and only at those instances the ticks are drawn, the times are kinda arbitrary. To make that prettier, based on the length of total duration, fixed time intervals can be chosen. I used an if-else statement to achieve this. Then decided to approach the problem by creating two arrays one for the intervals, and one for the total durations. Based on the total time length in the totalDuration array, the corresponding tick intervals will be chosen from the tickInterval array. The ticks will be drawn at these instances which are at predefined distances from each other. This should make the plot look better. Let’s do that:

%% Make the ticks even prettier
% if max(ts) - min(ts) < 101
%     stepSize = 10;
% elseif max(ts) - min(ts) < 201;
%     stepSize = 20;
% elseif max(ts) - min(ts) < 301
%     stepSize = 30;
% elseif max(ts) - min(ts) < 601
%     stepSize = 60;
% end

% Or a more elegant way could be:
tickIntervals = [10, 20, 30, 40, 60, 80, 90];
totalDuration = [101, 201, 301, 401, 601, 801, 1001];
indexInterval = find( max(ts) - min(ts) < totalDuration, 1);
stepSize = tickIntervals(indexInterval);

g = min(ts):stepSize:max(ts);
hAx.XTick = g;
hAx.XTickLabel = char(t(ismember(ts,g)));
hAx.FontSize = 24;
hAx.XLabel.String = 'Time (MM:SS)';


We got what we aimed for. However, what to do if let’s say the measurements were taken over 5 second intervals?

%% Regular Figure
hFig = figure;
hAx = gca;
ts = 0:5:0+length(d)*5;

stairs(ts(2:end), d, 'LineWidth', 2)
% ... The rest of code is same as above.

Here the time increments are in 5 seconds, and the data is collected is same as before. The first part of the code for plotting is shown above, where the rest of the code is exactly the same as before. At the end we will reach a plot as follows:


PS: If you have time data and want to plot the graph starting from a certain time instance and not zero, all you need to do is change ts = 0:length(d); to ts = 0+startTimeSeconds:length(d)+startTimeSeconds;. Here the start time is represented in seconds and added to the ts sequence creation. Below I will give an example of such a situation using the 2nd scenario with 5 second intervals. Here I’ll use a start time of 300 seconds, or 5 minutes.

%% Regular Figure
startTimeSeconds = 300;
hFig = figure;
hAx = gca;
ts = startTimeSeconds:5:startTimeSeconds + length(d)*5;

stairs(ts(2:end), d, 'LineWidth', 2)
% ... The rest of code is same as above.

The rest of the code is again the same. At the end we’ll reach the following graph:


Finally we have reached our goal of having a nice plot with time shown on the x axis.

Hope this will be helpful to someone out there. Bye!

Ilyas Ustun


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s