Effacer les filtres
Effacer les filtres

Display time on boxplot

15 vues (au cours des 30 derniers jours)
Nguyen Bao
Nguyen Bao le 4 Mai 2018
Commenté : dpb le 6 Mai 2018
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 commentaires
dpb
dpb le 4 Mai 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 le 4 Mai 2018
Modifié(e) : dpb le 4 Mai 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.

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 4 Mai 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 commentaires
dpb
dpb le 6 Mai 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 le 6 Mai 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...

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by