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
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.
Nguyen Bao
Nguyen Bao on 4 May 2018
Edited: dpb on 4 May 2018
Sorry, here for example:
a=[1 2 3 4 5 6 7 8; 4 5 6 1 1 1 1 8; 6 7 8 2 2 2 2 2];
t=[2000 2000.25 2000.5 2000.75 2001 2001.25 2001.5 2001.75];
figure
boxplot(a,'Labels',t','PlotStyle','compact');
But if there are many quarters, we need just display years or every two years to improve the readability.

Sign in to comment.

 Accepted Answer

dpb
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
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

[Moved from Answer by NB--dpb]
Many thanks DP. I need more time to get your idea of
dtm=datetime(t(1),1+[0:3:(t(end)-t(1))*4*3].',1);
dpb
dpb on 5 May 2018
Edited: dpb on 5 May 2018
It's just one of many ways to create a datetime vector that matches your time vector as quarters beginning at beginning--it's the three-element datevector [y,m,d] where the year and day are fixed to the beginning year of your series and the first day of the month, respectively. Then the month vector is just January to start (1) plus every three months (which is one quarter of year) until the last quarter is as many quarters as there are in your series (4/yr times 3 mo/quarter) to add up to the values in your series that are in years.
One could just as well have written it using date strings or any other allowable syntax; I just did it the above way to match your existing data sorta' automagically given the data you already had in the form you had it and to illustrate using the vector of months as input to generate a vector of outputs as tutorial. It is always much better to use integer fractions of the calendar as above than as fractions of the higher granular unit because floating point rounding can introduce differences for comparisons, etc., later on if try to match specific dates. datenum are more sensitive to that issue than are datetime, but is still best practice.
dpb
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
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...

Sign in to comment.

More Answers (0)

Categories

Asked:

on 4 May 2018

Commented:

dpb
on 6 May 2018

Community Treasure Hunt

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

Start Hunting!