MATLAB Answers

BoxPlot with datetime in Y-axis?

17 views (last 30 days)
Ireshika MAST
Ireshika MAST on 18 Apr 2019
Edited: Adam Danz on 19 Dec 2020
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)

Accepted Answer

Adam Danz
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? "
Convert the datetime to numeric and then use datetick() to display in date format.
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)

Tags

Community Treasure Hunt

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

Start Hunting!