2 boxplots overlayed - how to

3 ビュー (過去 30 日間)
Ana Campos
Ana Campos 2022 年 11 月 2 日
コメント済み: Ana Campos 2022 年 11 月 5 日
I have two boxplots created from information that the code is reading from different tables named "dataraw_BPM_XXXX".
Each boxplot is reading information from two columns ('subject ID' and 'Value') of one of the tables.
Code below for reference:
figure (3)
boxplotMeanRelaxing_SubjectID = dataraw_BPM_RELAXED.SUBJECT_ID
boxplotMeanRelaxing_Value = dataraw_BPM_RELAXED.VALUE
boxplot(boxplotMeanRelaxing_Value ,boxplotMeanRelaxing_SubjectID)
title('BPM readings per subject condition A')
xlabel('Subject ID')
ylabel('BPM readings during condition A')
figure (4)
boxplotMeanAROUSING_SubjectID = dataraw_BPM_AROUSED.SUBJECT_ID
boxplotMeanAROUSING_Value = dataraw_BPM_AROUSED.VALUE
boxplot(boxplotMeanAROUSING_Value ,boxplotMeanAROUSING_SubjectID)
title('BPM readings per subject condition B')
xlabel('Subject ID')
ylabel('BPM readings during condition B')
I'd like to overlay the two boxplots in a way that allows me to make comparisons between data obtained for conditions A and B. (a side by side option would be great - I've looked at some examples in the forum but unfortunately they were not what I need)
So far I only managed to overlay them and create a mess of overlayed boxes unfortunately.
Anyone knows how to help please?
Thanks very much.


Walter Roberson
Walter Roberson 2022 年 11 月 2 日
You can use hold on and then boxplot() again.
The second boxplot may use the same colors as the first, which is probably going to be confusing. However there is a 'colors' option that can be used to change the box colors. There is also a 'GroupColor' option to allow the colors to be controlled per group.
See also help boxplot which contains the useful hint:
To modify the properties of box components, use findobj using tags to
find their handles as in one of the examples below. The tag names
depend on the plotstyle and are:
all styles: 'Box', 'Outliers'
traditional: 'Median', 'Upper Whisker', 'Lower Whisker',
'Upper Adjacent Value', 'Lower Adjacent Value',
compact: 'Whisker', 'MedianOuter', 'MedianInner'
when 'notch' is 'marker':
'NotchLo', 'NotchHi'
To make it easier to distinguish which handle is from which boxplot(), you can use an additional trick that is not documented in help or doc but which is present in the source code. If you record the output of boxplot,
h = boxplot(boxplotMeanAROUSING_Value ,boxplotMeanAROUSING_SubjectID)
then you will get back what appears to be a numeric array. However if you then handle(h) then the numeric array will turn into an array of line() objects, one column per group. The source code describes the exact order of the handles, but I would suggest that you instead use that array of handles as the starting point for the findobj() described in help boxplot instead of counting on the order remaining the same.
  1 件のコメント
Ana Campos
Ana Campos 2022 年 11 月 5 日
Thanks very much for your help and time. I’ll try it today and see if I can make it work like that :)


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by