Clear Filters
Clear Filters

Plot multiple time ranges on a single line?

4 views (last 30 days)
Hello,
I have a Nx2 timetable of start (column 1) and end (column 2) times, read from a csv file. Each of these time ranges describe a session of data collection for location 'A' An example is this:
A =
'04/19/0022 14:37' '04/19/0022 22:49'
'05/03/0022 14:47' '05/03/0022 23:01'
'05/17/0022 16:58' '05/17/0022 23:02'
'05/31/0022 15:02' '05/31/0022 23:07'
'06/14/0022 14:33' '06/14/0022 23:03'
'10/26/0022 23:28' '10/28/0022 14:14'
'11/02/0022 23:58' '11/04/0022 00:18'
'11/14/0022 17:07' '11/16/0022 01:11'
How might I best plot this data in a time series so each collection session shows as a bar/whisker line on one horizontial axis within the plot?
Additionally, if I had another location and associated variable, 'B', how would I add this second horizontial line with sessions into the graph?
I've drawn an example here (the times don't match to the above data):
I have considered Gantt charts but this seems to be built more specifically towards different mixing of 'A' and 'B' between the horizontial planes.
Thank you.

Accepted Answer

Kyoung Moon Lee
Kyoung Moon Lee on 3 Feb 2023
I have created a code based on the values you wrote down and the picture attached.
You can edit this code by adding the data you need.
%% data
A = {
'04/19/2022 14:37' '04/19/2022 22:49'
'05/03/2022 14:47' '05/03/2022 23:01'
'05/17/2022 16:58' '05/17/2022 23:02'
'05/31/2022 15:02' '05/31/2022 23:07'
'06/14/2022 14:33' '06/14/2022 23:03'
'10/26/2022 23:28' '10/28/2022 14:14'
'11/02/2022 23:58' '11/04/2022 00:18'
'11/14/2022 17:07' '11/16/2022 01:11'};
for ii = 1:height(A)
dnum(ii,1) = datenum(A{ii,1});
dnum(ii,2) = datenum(A{ii,2});
end
%% figure
figure()
for ii = 1:height(A)
% set
hold on;
x = [dnum(ii,1) dnum(ii,2)];
y = [6 6];
% plot
plot(x,y,'k','LineWidth',5)
% deco
xlim([dnum(1,1) dnum(end,2)])
datetick('x','mm-dd','keeplimits','keeplimits')
xlabel('mm-dd (2022)')
ylim([0 10])
yticks([4 6])
yticklabels({'B','A'})
end
  1 Comment
Weston Hustace
Weston Hustace on 5 Feb 2023
Thank you! That helped me move in the right direction and your code was a great sample.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!