BoxPlot with datetime in Y-axis?
11 views (last 30 days)
Show older comments
How can I draw a boxplot with datetime value in the y axis? (ex: if arrival times of a vehicle is available over a year, to show the mean, median arrival times)
0 Comments
Accepted Answer
Adam Danz
on 18 Apr 2019
Edited: Adam Danz
on 19 Dec 2020
"How can I draw a boxplot with datetime value in the y axis? "
arrivalTime = datetime('Jan 01, 2001 12:00') : 0.2 : datetime('Jan 01, 2002 12:00');
boxplot(datenum(arrivalTime));
datetick('y') %more options: https://www.mathworks.com/help/matlab/ref/datetick.html
"if arrival times of a vehicle is available over a year, to show the mean, median arrival times"
As the matlab documentation indicates, boxplot() displays the median value. You'll have to show the mean value manually.
hold on
plot(1, mean(datenum(arrivalTime)), '*', 'DisplayName', 'Mean')
legend()
Instead of manually specifying x=1 for the center of the box plot, you could get the boxplot centers using,
bh = findobj(gca,'type','line','tag','Box'); % handles to rectangular boxes
xdata = vertcat(bh.XData);
boxCenters = (max(xdata,[],2) - min(xdata,[],2))/2 + min(xdata,[],2);
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!