I am thinking about locating MEAN as a straight horizontal line or a symbol inside the boxplots for illustration purposes. I would say it is possible since distributions are not significantly skewed. Any suggestions?!
Here is a MATLAB sample code:
rng default % For reproducibility
x1 = normrnd(5,1,100,1);
x2 = normrnd(6,1,100,1);
figure
boxplot([x1,x2],'Notch','on','Labels',{'mu = 5','mu = 6'})
title('Compare Random Data from Different Distributions')
cust_colr = [0, 0.5, 1
0.60156, 0.80078, 0.19531
0.5, 0, 0];
h = findobj(gca,'Tag','Box');
for j=1:length(h)
patch(get(h(j),'XData'),get(h(j),'YData'),cust_colr(j,:));
end
Output picture is shown below:

 採用された回答

michio
michio 2016 年 10 月 14 日

3 投票

The ability to plot the mean values using boxplot is not available as of release R2016b. To work around this issue, you can find these values and plot them manually. The example below shows how to plot the mean value of each group:
% Generate random data
X = rand(10);
% Create a new figure and draw a box plot
figure;
boxplot(X)
% Overlay the mean as green diamonds
hold on
plot(mean(X), 'dg')
hold off

8 件のコメント

Mohammed
Mohammed 2016 年 10 月 14 日
Thanks for your answer! If MATLAB plot command is a 2-D function, do you know how it retrieves the x-axis values in this scenario?!
michio
michio 2016 年 10 月 14 日
You are welcome. I am not so sure what you mean by "how to retrieve the x-axis values". What values are you referring to?
Mohammed
Mohammed 2016 年 10 月 14 日
If MATLAB plot command is a 2-D function, don't you think we need pair of values to plot green diamonds? In your case, you just plot them with one value.
michio
michio 2016 年 10 月 15 日
I see. If you look at the documentation page of plot function, for example, it states
"plot(Y) creates a 2-D line plot of the data in Y versus the index of each value. If Y is a vector, then the x-axis scale ranges from 1 to length(Y)."
So 2D function does accept only one value and its x-values is going to be just the index of each value. Hope it answers your concern.
Mohammed
Mohammed 2016 年 10 月 15 日
編集済み: Mohammed 2016 年 10 月 15 日
You did well. That's what I anticipated. Thanks again!
michio
michio 2016 年 10 月 15 日
Glad it helped;)
JIAYING WU
JIAYING WU 2020 年 8 月 29 日
but the means do includes outliers if there are any?
Bárbara Antonucci
Bárbara Antonucci 2021 年 9 月 7 日
Is functionally. Very great!

サインインしてコメントする。

その他の回答 (1 件)

Camilo Cárdenas
Camilo Cárdenas 2022 年 4 月 13 日

0 投票

Hi, thank you for your post.
I have got a question:
What aboout, if you have more than one box in a Diagramm? How can you assingn it to each group of data?
Thanks!

2 件のコメント

Simon Diaz
Simon Diaz 2022 年 9 月 28 日
Hi, try to
% Generate random data
X = rand(10,4);
% Create names of each group
names = {'name1','name2','name3','name4'};
% Create a new figure and draw a box plot
figure;
boxplot(X,'Labels',names)
% Overlay the mean as green diamonds
hold on
plot(1:length(X(1,:)),mean(X), 'dg') % x-axis is the intergers of position
hold off
JB676
JB676 2024 年 4 月 2 日
編集済み: JB676 2024 年 4 月 2 日
mu_T = % set of values for category 1
mu_RN = % set of values for category 2
mu_T_RN, grp_T_RN = % combined dataset values and labels
hb = boxplot(mu_T_RN,grp_T_RN,'whisker',1);
% replace median with mean value
set(hb(6,1),'ydata',[mean(mu_T) mean(mu_T)]);
set(hb(6,2),'ydata',[mean(mu_RN) mean(mu_RN)]);
Get the handle to the boxplot, then adjust item 6 'ydata'. For a two category example:

サインインしてコメントする。

質問済み:

2016 年 10 月 13 日

編集済み:

2024 年 4 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by