histogram of datetimes with customisable bin width
33 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
How to plot a histogram of (differences of) datetimes, with a customizable bin width, such as 1 minute ?
This is my attempt, but something is wrong/missing.....
% input
a = datetime({'2022-05-17 16:07:28.280'
'2022-05-17 16:08:55.968'
'2022-05-17 16:19:26.632'
'2022-05-17 16:21:23.840'
'2022-05-17 16:22:47.540'
'2022-05-17 16:25:01.152'
'2022-05-17 16:26:27.728'
'2022-05-17 16:32:47.192'
'2022-05-17 18:00:01.340'
'2022-05-17 18:05:41.080'
'2022-05-17 18:07:11.248'
'2022-05-17 18:08:59.208'
'2022-05-17 18:12:17.940'
'2022-05-17 18:19:33.520'
'2022-05-17 18:21:10.020'
'2022-05-17 18:22:20.740'
'2022-05-17 18:23:55.900'
'2022-05-17 18:25:05.460'
'2022-05-17 18:29:46.080'
'2022-05-17 20:02:22.160'
'2022-05-17 20:03:38.900'
'2022-05-17 20:05:17.112'
'2022-05-17 20:19:10.072'
'2022-05-17 20:20:38.928'
'2022-05-17 20:21:41.000'
'2022-05-17 20:23:16.260'
'2022-05-17 20:24:27.460'
'2022-05-17 20:28:48.980'});
% histcounts
b = diff(a);
binLimits = [min(b) max(b)];
binWdith = datetime('00:01:00');
numBins = ceil(minutes(diff(binLimits))/minute(binWdith));
[counts,binEdges]=histcounts(b, 'BinLimits',binLimits,'BinWidth',binWdith);
% histogram
histogram(b,'binEdges',binEdges,'FaceColor','k','EdgeColor','k');
0 Kommentare
Akzeptierte Antwort
Star Strider
am 20 Okt. 2022
After doing a few conversions between datetime and duration to get all the arguments to be what the functions want —
% input
a = datetime({'2022-05-17 16:07:28.280'
'2022-05-17 16:08:55.968'
'2022-05-17 16:19:26.632'
'2022-05-17 16:21:23.840'
'2022-05-17 16:22:47.540'
'2022-05-17 16:25:01.152'
'2022-05-17 16:26:27.728'
'2022-05-17 16:32:47.192'
'2022-05-17 18:00:01.340'
'2022-05-17 18:05:41.080'
'2022-05-17 18:07:11.248'
'2022-05-17 18:08:59.208'
'2022-05-17 18:12:17.940'
'2022-05-17 18:19:33.520'
'2022-05-17 18:21:10.020'
'2022-05-17 18:22:20.740'
'2022-05-17 18:23:55.900'
'2022-05-17 18:25:05.460'
'2022-05-17 18:29:46.080'
'2022-05-17 20:02:22.160'
'2022-05-17 20:03:38.900'
'2022-05-17 20:05:17.112'
'2022-05-17 20:19:10.072'
'2022-05-17 20:20:38.928'
'2022-05-17 20:21:41.000'
'2022-05-17 20:23:16.260'
'2022-05-17 20:24:27.460'
'2022-05-17 20:28:48.980'});
% histcounts
b = diff(a);
dtb = datetime(string(b));
binLimits = [min(dtb) max(dtb)];
binWdith = duration('00:01:00');
numBins = ceil(minutes(diff(binLimits))/minutes(binWdith));
[counts,binEdges]=histcounts(dtb, 'BinLimits',binLimits,'BinWidth',binWdith);
% histogram
histogram(dtb,'binEdges',binEdges,'FaceColor','k','EdgeColor','k');
Is that the desired result?
.
3 Kommentare
Cris LaPierre
am 20 Okt. 2022
Since b is created already using the datetime function, the following step is not necessary
dtb = datetime(string(b));
Weitere Antworten (1)
Cris LaPierre
am 20 Okt. 2022
By taking the diff of a, your variable b is now a duration, not a datetime. That means your binWidth must be of the same datatype. Make that a duration, and you code works.
I didn't see a need for numBins, so I removed that line of code.
% input
a = datetime({'2022-05-17 16:07:28.280'
'2022-05-17 16:08:55.968'
'2022-05-17 16:19:26.632'
'2022-05-17 16:21:23.840'
'2022-05-17 16:22:47.540'
'2022-05-17 16:25:01.152'
'2022-05-17 16:26:27.728'
'2022-05-17 16:32:47.192'
'2022-05-17 18:00:01.340'
'2022-05-17 18:05:41.080'
'2022-05-17 18:07:11.248'
'2022-05-17 18:08:59.208'
'2022-05-17 18:12:17.940'
'2022-05-17 18:19:33.520'
'2022-05-17 18:21:10.020'
'2022-05-17 18:22:20.740'
'2022-05-17 18:23:55.900'
'2022-05-17 18:25:05.460'
'2022-05-17 18:29:46.080'
'2022-05-17 20:02:22.160'
'2022-05-17 20:03:38.900'
'2022-05-17 20:05:17.112'
'2022-05-17 20:19:10.072'
'2022-05-17 20:20:38.928'
'2022-05-17 20:21:41.000'
'2022-05-17 20:23:16.260'
'2022-05-17 20:24:27.460'
'2022-05-17 20:28:48.980'});
% histcounts
b = diff(a);
binLimits = [min(b) max(b)];
binWidth = duration('00:01:00');
[counts,binEdges]=histcounts(b, 'BinLimits',binLimits,'BinWidth',binWidth);
% histogram
histogram(b,'binEdges',binEdges,'FaceColor','k','EdgeColor','k');
3 Kommentare
Siehe auch
Kategorien
Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!