フィルターのクリア

How to add Mean and Median to a boxplot?

62 ビュー (過去 30 日間)
Behrad Ze
Behrad Ze 2023 年 11 月 9 日
移動済み: Dyuman Joshi 2023 年 11 月 9 日
I am ploting a boxplot using inserting data from an Excel file.The excel file is attached. I want add Mean, Median and their legends to the Boxplot. The code I am using is as below:
sample=readmatrix(file.name)
x=sample(1:end,1);
xString=string(x);
dataSample=sample(1:end,2:end)
boxplot(dataSample)
and The plot is like This:
My problem is that I need both Median and Mean of the data for each series of the data. However the plot is only showing the Median. Can any one help me with adding a line for Mean like the one was added for Median? Thank you

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 9 日
移動済み: Dyuman Joshi 2023 年 11 月 9 日
sample=readmatrix('Data.csv');
x=sample(1:end,1);
xString=string(x);
dataSample=sample(:,2:end);
%Calculate mean
m = mean(dataSample, 'omitnan');
%plot the boxchart and get the handle to the boxchart object
b = boxchart(dataSample);
%get the width of the boxes;
width = b.BoxWidth;
%position data of boxes
x = double(b.XData);
%Data to plot individual lines for mean values
X = [x-width/2; x+width/2];
avg = [m;m];
hold on
%plot the mean values
plot(X,avg,'r')
  1 件のコメント
Behrad Ze
Behrad Ze 2023 年 11 月 9 日
移動済み: Dyuman Joshi 2023 年 11 月 9 日
It works. Thanks for the answer

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

その他の回答 (0 件)

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by