MATLAB: Display time on boxplot

boxplottime series

Hi every one This is my boxplot with time (quarterly) on x-axis, using time series as 'Labels'. I want to improve the readability by display years only or any adjustment so that we can see the dates properly. Please advise me! Many thanks.

Best Answer

boxplot is another of the specialty plots that's somewhat hard to work with; TMW has a recent penchant for burying details that would be obvious user-dependent/modifiable things where hard to get at...ideally, one could actually use an axis as a time axis and with enough effort one might be able to forcefeed the underlying ruler object but it isn't a timeruler so would have to use datenum. A workaround at the top level would be something like--
dtm=datetime(t(1),1+[0:3:(t(end)-t(1))*4*3].',1); % create datetime to match times given
Labels=cellstr(repmat(' ',length(a),1)); % make an empty labels array for boxplot
Labels(1:2:end)=cellstr(dtm(1:2:end)); % fill every second with a time string
close
figure
subplot(2,1,1)
boxplot(a,'Labels',Labels,'plotstyle','compact') % default format
subplot(2,1,2)
dtm.Format='yyQQQ'; % more concise year/Quarter format
Labels(1:2:end)=cellstr(dtm(1:2:end)); % update the Labels array to match
boxplot(a,'Labels',Labels,'plotstyle','compact')
gives
You can choose whatever fraction of the labels to show desired; was hoping if could use an x-axis itself to use the datetime array as an input and would get autoscaling but that isn't supported at the top level. NB: there has to be a label for every column in a; hence the array of blank cellstrings to begin with.