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

17 ビュー (過去 30 日間)
%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");

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 7 月 13 日
編集済み: Cris LaPierre 2021 年 7 月 13 日
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 件のコメント
Maria Baeza
Maria Baeza 2021 年 7 月 13 日
Cris,
Thank you very much for providing insight in how you arrived at your solution. It worked well and it helped me understand and learn reading your comments and text.
Originally the data came from a timekeeping system that was imported into Excel. Instead of completing the job in Excel, I am trying to use it as an opportunity to learn MatLab, so I realize it was messy..
Thank you for helping me
Cris LaPierre
Cris LaPierre 2021 年 7 月 13 日
How do you import the spreadsheet? Can you attach the original Excel file?

サインインしてコメントする。

その他の回答 (2 件)

Akira Agata
Akira Agata 2021 年 7 月 12 日
編集済み: Akira Agata 2021 年 7 月 12 日
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 件のコメント
Maria Baeza
Maria Baeza 2021 年 7 月 12 日
Thank you for your quick response. When I input the suggested changes (below), the Y Axis show timing information as I would like but the boxplot no longer appears? The information is type: datenum as should be in in the range of 30 seconds to 3 minutes.. which I think the label now shows but maybe that is hours?
Any suggestions would be greatly welcomed.
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");
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)');
subtitle('Age: 11-12',FontName = 'Arial', FontSize = 10, FontWeight = 'Bold');

サインインしてコメントする。


Cris LaPierre
Cris LaPierre 2021 年 7 月 12 日
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).
The solution may be to use ytickformat instead. It supports single-digit minute formats (see here).
ytickformat('m:ss')
  7 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 7 月 13 日
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)))

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by