Trying to set Y-Axis to Minutes:Seconds for swim times. Would like to set range from 0 to 3:30 and eliminate leading zeros
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Maria Baeza
am 11 Jul. 2021
Kommentiert: Maria Baeza
am 13 Jul. 2021
%Group4_Statistics are datenums
boxplot(Group4_Statistics,'Labels',{'IM_100','Free_50', 'Breast_50','Back_50', 'Fly_50'});
title('2021 Times by Stroke', FontName = 'Arial', FontSize = 12, FontWeight = 'Bold');
ylabel('Time');
datetick('y',"MM:SS");
0 Kommentare
Akzeptierte Antwort
Cris LaPierre
am 13 Jul. 2021
Bearbeitet: Cris LaPierre
am 13 Jul. 2021
Boxplot (and boxchart) do not yet support datetimes (see here), so you need to work with numeric values (like datenums), as you are doing already. ytickformat will not work. To display ylabels in "m:ss" format, you will need to manually create a string array containing your labels and manually update the ylabels.
I elected to convert your datenums to minutes, as I found it easier to work with 0-3.5 instead of 738157.0000000000 - 738157.0024305555.
I also notice these plots automatically scale the y axis, so you need to explicitly set the limits you want to have. Finally, only datetimes support a "m:ss" format, so I elected to create a vector of datetimes, set the format, and convert to string to create the ylabels.
load Swimming-Group4.mat
% convert data to times
tms = timeofday(datetime(Group4_Statistics,'ConvertFrom',"datenum"));
% plot using time converted to decimal minutes.
boxplot(minutes(tms),'Labels',{'IM_100','Free_50', 'Breast_50','Back_50', 'Fly_50'});
% set the y axis ticks and limits. Values represent time in minutes
yticks(0:0.5:3.5)
ylim([0 3.5]) % without this, the 0 and 3.5 ticks are not visible.
% create ylabels in m:ss format
yticklabels(string(datetime(2021,1,1,0,0,0,'Format',"m:ss")+minutes(0:0.5:3.5)))
I noticed you are only interested in the times. I'm curious how you collected your data to include a date as well. I suspect these times were not collected on Jan 1, 2021 at midnight. To help make the yaxis values meaningful, I stripped away the date info using the timeofday function. I suspect that my code looks more complicated because I'm also formatting the data to what I want it to be. However, if the data were just time (durations, or time measured in seconds or decimal minutes) some of this would not be necessary. For more, see the Represent Dates and Times in MATLAB page.
2 Kommentare
Cris LaPierre
am 13 Jul. 2021
How do you import the spreadsheet? Can you attach the original Excel file?
Weitere Antworten (2)
Akira Agata
am 12 Jul. 2021
Bearbeitet: Akira Agata
am 12 Jul. 2021
How about the following solution?
% Sample data
t = 0.25+3*rand(100,1);
% Create boxplot
figure
boxplot(t)
d = 0:0.5:3.5;
ax = gca;
ax.YLim = [0 3.5];
ax.YTick = d;
ax.YTickLabel = compose('%d:%02d',floor(d)',mod(d*60,60)');
ylabel('Time','FontSize',12);
1 Kommentar
Cris LaPierre
am 12 Jul. 2021
Your format spec is forcing two digits for the minutes ('MM:SS'). The way to eliminate a leading zero is to only use one 'M'. However, it doesn't look like datetick supports that (see dateFormat inputs here).
ytickformat('m:ss')
7 Kommentare
Cris LaPierre
am 13 Jul. 2021
Since a boxplot/boxchart needs the values to be numeric, I think Example 1 is the best way to save the data. Example 2 mixes formats (some mm:ss.SS, some ss.SS) so that file would require more code.
You do not want to include 0's, so I convert them all to NaN.
In the end, the plotting code is not any simpler because I still need the data to be numeric, and since MATLAB cannot yet import a duration that does not use a colon to separate times.
% import data
ex1 = readtable("Example 1 - 2021 - Lake Olympia Swim Team - Swimmer Results.xlsx");
ex1 = standardizeMissing(ex1,0,"DataVariables",10:18);
% Create boxchart
boxchart(ex1{:,10:18})
xticklabels(ex1.Properties.VariableNames(10:18))
set(gca,'TickLabelInterpreter','none')
ylim([0 210])
yticks(0:30:210)
yticklabels(string(datetime(2021,1,1,0,0,0,'Format',"m:ss")+minutes(0:0.5:3.5)))
Siehe auch
Kategorien
Mehr zu Spreadsheets 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!