Display time on boxplot
13 views (last 30 days)
Show older comments
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/190302/image.jpeg)
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.
2 Comments
dpb
on 4 May 2018
What is the time data format used???? Show us code; better yet give us a complete sample to run so don't have to try to guess/make up stuff.
Accepted Answer
dpb
on 4 May 2018
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/188142/image.jpeg)
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.
4 Comments
dpb
on 6 May 2018
[Move OP's Answer to Comment...dpb]
Many thanks, now can play around with your code. But, more precisely, instead of just showing a date (year or quarter) I would like to show a period of time. It is because each boxplot is produced based on many previous observations up to that time, say tt. In order words, I want to replace 00Q1 with 99Q1-00Q1, indicating that the first boxplot is estimated based on the data from 99Q1-00Q1 (tt=4 quarters or year back). Similarly, the next label is 99Q3-00Q3, ... I know that we need to create a new series tt based on dmt but I don't know to put them together with the dash in between or somehow to make these intervals.
dpb
on 6 May 2018
That will require actually building a label string; there is no base format like that for datetime data. You'll have to build a vector of the year/quarters wanted and then write the string using the desired format. Grouping variables or the timeseries might be useful here to get the desired granularity, not sure; have to ponder that some...
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!