how do I add MEAN to Boxplot?

197 visualizzazioni (ultimi 30 giorni)
Mohammed
Mohammed il 13 Ott 2016
Commentato: Simon Diaz il 28 Set 2022
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:

Risposta accettata

michio
michio il 14 Ott 2016
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 Commenti
JIAYING WU
JIAYING WU il 29 Ago 2020
but the means do includes outliers if there are any?
Bárbara Antonucci
Bárbara Antonucci il 7 Set 2021
Is functionally. Very great!

Accedi per commentare.

Più risposte (1)

Camilo Cárdenas
Camilo Cárdenas il 13 Apr 2022
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!
  1 Commento
Simon Diaz
Simon Diaz il 28 Set 2022
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

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by